am making a simepe leaderstats that gives Points but I keep getting script Timeout
I have tried to fix it and I tried Solutions on the Developer Form IDK if it’s my wi fi or something
if this helps
local Players = game:GetService("Players")
local function yummy(player)
local leaderboard = Instance.new("Folder")
leaderboard.Name = "leaderstats"
leaderboard.Parent = player
local Points = Instance.new("IntValue")
Points.Name = "POINTS"
Points.Parent = leaderboard
end
yummy()
Players.PlayerAdded:Connect(yummy)
while true do
local point = Instance.new("Part")
point.Position = Vector3.new(math.random(1.1000),7,math.random(1.1000))
point.Touched:Connect(function(hit)
if point then
point:Destroy()
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
player.leaderstats.POINTS.Value = player.leaderstats.POINTS.Value + 1
wait(0.5)
end
end
end)
end
It’s your while loop. It does not yield, and therefore disrupts the execution of other threads due the nature of cooperative multitasking. Roblox kills your thread to stop the collateral damage. You’ll need to set a rate at which points are created. That, or only create a new point when one is deleted. You should consider setting a maximum number of points that can exist at any given time
Final Version might not be efficient at all but gets your job done.
local Players = game:GetService("Players")
local numberOfPoints = 0
local maxPoints = 500
local PointsFolder = workspace.PointsFolder
PointsFolder.ChildRemoved:Connect(function()
while numberOfPoints < maxPoints do
print("Started")
local newPart = Instance.new("Part")
newPart.Anchored = true
newPart.Position = Vector3.new(math.random(-150 , 150 ) , 3 , math.random(-150 , 150 ))
newPart.Parent = workspace.PointsFolder
numberOfPoints += 1
newPart.Touched:Connect(function(Hit)
local Player = Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
print("Player Touched")
newPart:Destroy()
numberOfPoints -= 1
-- add leader point or whatever you want--
end
end)
task.wait()
end
end)
while numberOfPoints < maxPoints do
print("Started")
local newPart = Instance.new("Part")
newPart.Anchored = true
newPart.Position = Vector3.new(math.random(-150 , 150 ) , 3 , math.random(-150 , 150 ))
newPart.Parent = workspace.PointsFolder
numberOfPoints += 1
newPart.Touched:Connect(function(Hit)
local Player = Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
print("Player Touched")
newPart:Destroy()
numberOfPoints -= 1
-- add leader point or whatever you want--
end
end)
task.wait()
end
This code still contains a bug. BasePart.Touched is an RBXScriptSignalobject, not a boolean. Scripts do not wait on if-statements either; “if newPart.Touched then” does not describe what should happen when a part is touched. An if-statement is evaluated only once and if its condition proves to be false, it will never be evaluated again unless otherwise scripted to. If the value changed to true, nothing would happen. Lua(u) has a loose defintion of what is true or false. This leads to the discernment being of “truthy” or “falsy”, meaning “of that nature”. Luau considers nil and false to be falsy, and all other things to be truthy. Since RBXScriptSignal objects aren’t falsy, it will be observed as true, leading to your if-statement always passing, regardless of whether or not something touched or is currently touching the target part
local Players = game:GetService("Players")
local Points = workspace.Points :: Folder
local PointsZone = Points.Zone :: BasePart
local PointPrefab = script.Point :: Point
local MAX_POINTS = 25
local UNIT_POINT_WORTH = 1
local RANDOM = Random.new()
local function onLivingPlayerTouched(part: BasePart, callback: (player) -> ()): RBXScriptConnection
return part.Touched:Connect(function(otherPart: BasePart)
local character = otherPart.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health <= 0 then
return
end
local player = Players:GetPlayerFromCharacter(character)
if not player then
return
end
callback(player)
end)
end
local function onPlayerAdded(player: Player)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
end
local points = Instance.new("IntValue")
points.Name = "Points"
points.Parent = leaderstats
leaderstats.Parent = player
end
local function makePoint()
local halfSize = PointsZone.Size / 2
local randomOffset = Vector3.new(
RANDOM:NextNumber(-halfSize.X, halfSize.X),
RANDOM:NextNumber(-halfSize.Y, halfSize.Y),
RANDOM:NextNumber(-halfSize.Z, halfSize.Z)
)
local point = PointPrefab:Clone()
point:PivotTo(CFrame.new(PointsZone.Position + randomOffset))
point.Parent = Points
onLivingPlayerTouched(point.Part, function(player: Player)
local leaderstats = player.leaderstats
local points = leaderstats.Points
points.Value += UNIT_POINT_WORTH
point:Destroy()
makePoint()
end)
end
for _ = 1, MAX_POINTS do
makePoint()
task.wait()
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAdded, player)
end
Players.PlayerAdded:Connect(onPlayerAdded)