Set Humanoid WalkSpeed - lua

I found this script in a similar question but, I'm getting the error 16:11:18.560 - Workspace.Script:2: attempt to index nil with 'Character'
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- setting speed
local Humanoid = character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
Can anyone help me?

The LocalPlayer object only exists in LocalScripts. That's why your variable Player is nil.
There are two ways you can fix this :
1) Move this code into a LocalScript, or
2) Add this code to a callback that is executed when a player joins the game. Here's what that would look like.
local PlayerService = game:GetService("Players")
-- wait for a player to join the game
PlayerService.PlayerAdded:Connect( function(Player)
-- wait for the player's character to load
Player.CharacterAdded:Connect( function(Character)
-- set the speed
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
end)
end)

Or make a brick that gives you speed:
local KillBrick = script.Parent()
KillBrick.Touched(function(kill)
local humanoid = kill.Parent:FindFirstChild("humanoid")
if humanoid then
humanoid.WalkSpeed = 25
end
end)

Related

roblox studio script problems

I am making a game where when a part at the front of a car touches another part, the other part becomes unanchored. I am making a different script that gives the player a coin every time they do this. Yes, I have already made a leaderstats script. code:
function onPartTouched(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
You do actually have to attach the function lol
function onPartTouched(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
MyPart.Touched:Connect(onPartTouched)

"attepted to index nil with Humanoid"

local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local human = char.Humanoid
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end
end)
this script keeps returning error "attempted to index nil with huamnoid" i have looked for solutions on dev forum and on stack overflow but i could not find anything so im going to post myself. anyone catch any erros?
"attempted to index nil with huamnoid"
What that means is that this script loaded before the player's character loaded in.
To fix this, you can use
local char = player.Character or player.CharacterAdded:Wait()
-- uses the player if it's already loaded in, or waits for the character to load in
Also make sure to use
local human = char:WaitForChild("Humanoid")
-- to make sure you wait till the humanoid gets added
Final script
local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end)

attempt to index nil with 'LoadCharacter'

When trying to respawn the player with plr:LoadCharacter() it just gives me:
attempt to index nil with 'LoadCharacter' & I tried multiple ways to do it such as Player:LoadCharacter() or is there a more efficient way to kill/respawn the player?
--Declared Boolean Global variable
_G.TimerStart = false
--Local Paths to Objeccts
local label = game.StarterGui.TimerGUI.Timer
-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
--Get Children
local Child = game.Players:GetChildren()
-- Wait for Child Variables
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")
local timerevent = ReplicatedStorage:WaitForChild("Timer")
local function Thieves(Players)
if _G.TimerStart == false then
for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
for i,v in pairs(game.Teams.Police:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
end
end
In this case plr is a nil value. So plr:LoadCharacter() is not allowed as it does not make any sense.
local plr = game:GetService("Players").LocalPlayer
is the reason.
So refer to this manual page: https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer
Maybe this helps:
Loading GUIs When creating loading GUIs using ReplicatedFirst, sometimes a LocalScript can run before the LocalPlayer is available.
In this case, you should yield until it becomes available by using
Instance:GetPropertyChangedSignal
local Players = game:GetService("Players")
-- Below: access Players.LocalPlayer; if it is nil, we'll wait for it using GetPropertyChangedSignal.
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

Getting a nil response while trying to clone a object to backpack

I am trying to clone an object from the replicatedstorage to the players backpack when an object part is touched and the code looks fine for me but it keeps giving a nil response from clone.parent = player.backpack
local replicatedtorage = game:GetService("ReplicatedStorage")
local Sword = replicatedtorage:FindFirstChild("Sword")
local part = game.Workspace.Part
local player = game.Players.LocalPlayer
local clone = Sword:Clone()
part.Touched:Connect(function(hit)
local humanoid = hit.parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
clone.Parent = player.Backpack
end
end)
This looks like a server Script which cannot access Players.LocalPlayer like clients can because there is no local player to the server. A way to get the Player that touched a part is through Players:GetPlayerFromCharacter() which requires one instance to be passed and will either return the Player whose Character is that instance or nil.
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
clone.Parent = player.Backpack
end
end)
This should work right away in your script and can replace your existing Touched connection.

For loop Failed

I have been trying to make a Roblox sword fight game (my first roblox game).
I found some syntax problems in my code but fixing them didn't fix this issue. I have been going over my code and nothing really seems to work.
Here's the 15th to the 83rd line of code becuase the comments have told me the problem is before the for loop (Before the 15th line are just variables)
--Game Loop
while true do
Status.Value = "Waiting for enough players"
repeat wait(1) until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(10)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player) -- Add each player into plrs table
end
end
wait(2)
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
--Teleport players to the map
local SpawnPoints = ClonedMap:FindChild("SpawnPoints")
if not SpawnPoints then
print("Spawnpoints not found!")
end
local AvailableSpawnPoints = SpawnPoints:GetChildren
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
-- Teleport them
character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
table.remove(AvailableSpawnPoints,1)
-- Give them a Sword
local Sword = ServerStorage.Sword:Clone()
Sword.Parent = player.Backpack
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
-- There is no character
if not player then
table.remove(plrs,i)
end
end
end
end
Here's the error:
19:30:03.021 - ServerScriptService.Main Script:56: Expected '(', '{' or , got 'for'
Help me out fellow gamers
#luther's comment is absolutely right. The line right above the for loop has a syntax error. SpawnPoints:GetChildren is a function call, and you forgot to add the parentheses.
local AvailableSpawnPoints = SpawnPoints:GetChildren()

Resources