Ok, still learning here but I can’t figure out what is causing the error "Attempt to Index local ‘player’ (a nil value) on the 3rd line.
Basically I am trying to check if/when a child(pet) is being added to the ‘PetsEquiped’ folder and if so then update the walk speed of the character. I have another script that updates this SpeedBoost value for the player already so I just wanted to update the walkspeed value now in this new script.
What am I missing here?
PS: Never used ChildAdded but I think the syntax is right.
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
player:WaitForChild("PetsEquiped").ChildAdded:Connect(function(character)
if player~=nil then
if player:FindFirstChild("SpeedBoost") then
print ("found speed boost")
local spboost = player:WaitForChild("SpeedBoost").Value
print("sp Boost is "..spboost)
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed + spboost
else
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed
end
end
print("walkspeed "..character.Humanoid.WalkSpeed)
end)
I can assume that you’re running in this in a server-sided script (normal script). Normal scripts don’t have a LocalPlayer property as the server is not associated with any clients as it runs on a server computer. The solution would be to use the Player added event to get the player as they join, and then you connect the ChildAdded event to the player’s “PetsEquipped”:
game.Players.PlayerAdded:Connect(function(player)
player:WaitForChild("PetsEquipped").ChildAdded:Connect(function(child)
-- code here
end)
end)
if this is in a script, you cannot index LocalPlayer on the server. Instead, you can run it in a LocalScript if you want to index the LocalPlayer.
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
should become:
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
I don’t know exactly how you add instances to the client, but I’d recommend adding them on server so the instances are replicated to the server and the values can be changed both on the client and the server.
The problem is pets aren’t equipped when the player joins the game, therefore this would never pick up that new pets that are added ‘x’ random time later.
So I was trying to use ChildAdded to detect when a pet was attached to the player and then to check if walkspeed needs to be updated.
I will check the syntax for ChildAdded again maybe there is another trigger i can use.