For some weird reason my script only work when playing inside the studio and did not work on local server and public server and i tried to fix it but i cant still get it to work, im still a beginner scripter
What im trying to make here is a distance script with a maximum distance from the center of map
This is the script that i placed on serverscriptserveice:
function onPlayerEntered(newPlayer)
wait(game.Loaded)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
local dist = Instance.new("IntValue")
dist.Name = "Distance"
dist.Value = 0
dist.Parent = stats
local max = Instance.new("IntValue")
max.Name = "Max"
max.Value = 0
max.Parent = stats
stats.Parent = newPlayer
while true do
wait(0.5)
dist.Value = math.round(newPlayer.Character.HumanoidRootPart.CFrame.Position.Z)
if dist.Value >= max.Value then
max.Value = dist.Value
end
end
end
game.Players.ChildAdded:connect(onPlayerEntered)
Also use Players.PlayerAdded
and if issue persists try wrapping the loop in a spawn function or coroutine
task.spawn(function()
while true do
wait(0.5)
dist.Value = math.round(newPlayer.Character.HumanoidRootPart.CFrame.Position.Z)
if dist.Value >= max.Value then
max.Value = dist.Value
end
end
end)
im surprised your script runs at all.
first of wait(game.Loaded) will not work because wait() expects a number and .Loaded is a event.
im pretty sure .Loaded is also client only.
your onPlayerEntered function expects a argument named newPlayer. you should do:
i tried using that but that dosent worked
here is the current script
function onPlayerEntered(newPlayer)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
local dist = Instance.new("IntValue")
dist.Name = "Distance"
dist.Value = 0
dist.Parent = stats
local max = Instance.new("IntValue")
max.Name = "Max"
max.Value = 0
max.Parent = stats
stats.Parent = newPlayer
while true do
wait(0.5)
dist.Value = math.round(newPlayer.Character.HumanoidRootPart.CFrame.Position.Z)
if dist.Value >= max.Value then
max.Value = dist.Value
end
end
end
game.Players.ChildAdded:Connect(function(player)
onPlayerEntered(player)
end)