So what I am trying to do is have the players X Y and Z coordinates saved to a data store, i have the saving part working, now i just need to write the data.
Except… my script is not saving the data.
I have tried doing things like wait for child and such but to no avail. Here is my script
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local root = char:WaitForChild("HumanoidRootPart")
local hum = char.Humanoid
local positionStats = plr:WaitForChild("leaderstats")
plr.CharacterAdded:Connect(function(character)
hum.Running:Connect(function(speed)
if speed > 0 then
positionStats.X = root.Position.X
positionStats.Y = root.Position.Y
positionStats.Z = root.Position.Z
end
end)
end)
I think the issue is at the start, when you find the character:
local char = plr.Character or plr.CharacterAdded:Wait()
You already repeated wait() until the character was added, meaning when you connect the .CharacterAdded event afterwards, it doesn’t run because the character has already been added.
I would recommend moving all of the character related variables inside of the
local plr = game.Players.LocalPlayer
plr.CharacterAdded:Connect(function(char)
local root = char:WaitForChild("HumanoidRootPart")
local hum = char.Humanoid
local positionStats = plr:WaitForChild("leaderstats")
plr.CharacterAdded:Connect(function(character)
hum.Running:Connect(function(speed)
if speed > 0 then
positionStats.Cords.Value = Vector3.new(root.Position)
end
end)
end)
end)
What you meant? If so its still not working. Its a local script if that matters?
Yeah that’s what I meant. I think it actually is because you’re using a local script now that I think about it. The local script only changes the server side, so it doesn’t register in the other server scripts that the position. In other words, after repeatedly updating the value in the local script, once you finally go to save it in another server script, the server script doesn’t see the changes you made in the local script.
Try moving it into a server script and doing it like this:
game.Players.PlayerAdded:Connect(function(plr)
-- And then do the rest in here
end)
ServerScriptService.getplayerpositions:24: attempt to index nil with 'FindFirstChild'
with script
game.Players.PlayerAdded:Connect(function(plr)
for i,v in pairs(game.Players:GetChildren()) do
local hum = v.Character:FindFirstChild("Humanoid")
local root = hum.Parent.HumanoidRootPart
local pos = root.Position
local leadersts = plr:WaitForChild("leaderstats")
plr.CharacterAdded:Connect(function(character)
hum.Running:Connect(function(speed)
if speed > 0 then
leadersts.Cords.Value = Vector3.new(root.Position)
end
end)
end)
end
end)
Assuming line 24 (in the error) is the line where you get the humanoid:
local hum = v.Character:FindFirstChild("Humanoid")
Then what it’s saying is that what you’re indexing, or what you’re trying to find the child of, does not exist. In other words, “v.Character” doesn’t exist, so it probably hasn’t loaded in yet.
With how you’re finding all of the players, you don’t need the whole for i,v in pairs loop to find each player. By having:
You’re essentially already getting all of the players, because that code runs for every player as they join the server. All you really need is something like this:
game.Players.PlayerAdded:Connect(function(plr)
local leadersts = player:WaitForChild("leaderstats") -- Get these values outside of the character added since I assume they're only created once when the player first joins the game.
local cords = leadersts:WaitForChild("Cords")
plr.CharacterAdded:Connect(function(character)
local hum = character:WaitForChild("Humanoid") -- I suggest using :WaitForChild() instead of :FindFirstChild() in case the humanoid hasn't loaded into the character yet as well.
local root = character:WaitForChild("HumanoidRootPart")
local pos = root.Position
hum.Running:Connect(function(speed)
-- And then just the updating in here
end)
end)
end)