When I was trying to make and run a script for coins in my game the output said "Script timeout: exhausted allowed execution time"
Script:
game.Players.PlayerAdded:Connect(function(player)
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
local coinvalue = Coins.Value
coinvalue = 0
Coins.Parent = player
wait(0.01)
if player.Name == "Vlo_tz" then
coinvalue = 25
end
wait(0.01)
local cointext = game.StarterGui.SideGuis.InventoryFrame.CoinsTextValue
while true do
cointext = coinvalue
end
end)
Your script is executing for too long without any kind of break.
The error is complaining that this loop has no exit case :
while true do
cointext = coinvalue
end
Adding a wait() inside the loop would get rid of the error, but it looks like you're using it to keep some kind of TextValue updated.
A safer way to do this is with event based callbacks. Instead of running a loop that will always try to update the cointext, you can listen for when the Coins value changes and then call a function to update it instead.
game.Players.PlayerAdded:Connect(function(player)
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = player
if player.Name == "Vlo_tz" then
Coins.Value = 25
end
-- update the gui whenever Coins changes
Coins.Changed:Connect(function()
-- find the player's copy of the UI, if it has loaded
-- (I'm assuming this is a TextValue and not a TextLabel)
local coinText = player.PlayerGui.SideGuis.InventoryFrame.CoinTextValue
-- keep this TextValue updated
coinText.Value = tostring(Coins.Value)
end)
end)
Related
local Players = game:GetService("Players")
local TouchPart = workspace["Start"]
local target = game.Workspace.Level1
local function teleport()
TouchPart.Touched:Connect(function(touched)
if touched.Parent:FindFirstChild("Humanoid") then
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
--Player.Character.HumanoidRootPart.CFrame = target.CFrame + Vector3.new(0, 5, 0)
print("Teleport")
end
end
end)
end
while true do
local zeit = game.Workspace.wall1.time.Value
zeit = zeit - 1
game.Workspace.wall1.time.Value = zeit
game.Workspace.wall1.SurfaceGui.Frame.TextLabel.Text = zeit
wait(1)
print(zeit)
if zeit == 0 then
teleport()
game.Workspace.wall1.time.Value = 20
end
end
The Function Teleport should only be called if zeit == 0. And print("Teleport") should only work when zeit hits 0 and the player is touching the part.
My Problem is even if zeit isnt 0 the function teleport() prints "Teleport" into the console when a player touches the part. Am i missing something there ?
Your issue is that you are connecting a Touched listener every time zeit reaches zero. This listener will fire from here on out, until you disconnect it. It also means that after two loops, it will print "teleport" twice, and after three you will see it printed three times with every touch.
You either need to disconnect the listener, or simply teleport the people touching the part when the timer strikes zero. Here's how you would do the latter :
local Players = game:GetService("Players")
local TouchPart = workspace["Start"]
local target = game.Workspace.Level1
local zeit = game.Workspace.wall1.time
local wallText = game.Workspace.wall1.SurfaceGui.Frame.TextLabel
-- function for teleporting all of the players currently touching the TouchPart
local function teleport()
local touchingPlayers = {}
local parts = TouchPart:GetTouchingParts()
for _, touched in ipairs(parts) do
local Player = Players:GetPlayerFromCharacter(touched.Parent)
if Player then
touchingPlayers[Player] = touched.Parent
end
end)
-- teleport all the touching players
for player, character in pairs(touchingPlayers) do
print("Teleporting " .. player.Name)
-- character:PivotTo(target.CFrame + Vector3.new(0, 5, 0))
end
end
-- keep the wall text constantly updated
zeit.Changed:Connect(function(newValue)
wallText.Text = tostring(newValue)
end)
-- update the counter every second
while wait(1) do
zeit.Value -= 1
if zeit.Value == 0 then
teleport()
wait(1)
zeit.Value = 20
end
end
I can not resolve this by my self.
Thank You for helping.
This is my first stackoverflow question :3
Roblox Studio information:
Latest available version as of 2022 October 03
Picture of about window:
https://i.stack.imgur.com/Wo6Br.png
https://i.stack.imgur.com/v7Jot.png
The output:
ServerScriptService.CheckpointsScript:31: attempt to index nil with 'leaderstats'
The code:
local Players = game:GetService("Players")
local CheckpointsFolder = game.Workspace.Checkpoints
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Parent = leaderstats
--player.CharacterAdded:Connect(function(char)
-- local checkpoint = CheckpointsFolder:FindFirstChild(Stage.Value)
-- player.Character..CFrame = CFrame.new(checkpoint.PrimaryPart.CFrame.Position.Y + 5 =, checkpoint.PrimaryPart.CFrame.Rotation)
--end)
end)
for _, v in pairs(CheckpointsFolder:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(part)
local player = Players:GetPlayerFromCharacter(part.Parent)
local stageValue = player.leaderstats.Stage
if player and stageValue.Value < tonumber(v.Name) then
stageValue.Value = v.Name
end
end)
end
end
Your issue is coming from this line
local stageValue = player.leaderstats.Stage
And that is because the Touched event fires for any object that touches it. And you need to make sure that player actually exists.
You are doing this already, you just need to move this line inside your safety check.
local player = Players:GetPlayerFromCharacter(part.Parent)
if player then
local stage = player.leaderstats.Stage
local currentStage = tonumber(v.Name)
if stage.Value < currentStage then
stage.Value = currentStage
end
end
I was trying to re-edit the code over and over again but it still didn't work I've created the folder leader stats and when I play the game it shows that it's a part of the player. It says however that it isn't a valid member.
The other error says: Cash is not a valid member of Folder "Players.(players name).leaderstats"
It's because game.Players.PlayerAdded is an event which you're assigning to a variable.
Try this for the PlayerAdded script (you will need that cash add function in this script):
local players = []
game.Players.PlayerAdded:Connect(function(ply)
table.insert(players, ply)
end)
while true do
wait(60)
for i, v in ipairs(players) do
v:WaitForChild("leaderstats").Counter.Value += 1
end
end
I've written this from my memory as I am away from a PC that can test this code so best of luck!
while wait(60) do
for i, v in ipairs(game.Players:GetChildren()) do
if v:FindFirstChild("leaderstats") then
if v.leaderstats:FindFirstChild("Counter") then
v.leaderstats.Counter.Value += 1
end
end
end
end
You always need to make sure what you're using exists. If you want to avoid errors, I prefer using FindFirstChild instead of WaitForChild to not get it into an infinite wait incase it somehow doesn't load.
it looks like you forgot to create the leaerstats folder. Here is a fixed code:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
while true do
task.wait(60)
Counter.Value += 1
end
end)
This will start counting time after player joins. If you want to increase counter value of all players at the same time, use this code:
local PlayersService = game:GetService("Players")
local Players = {}
PlayersService.PlayerAdded:Connect(function(player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = player
local Counter = Instance.new("IntValue")
Counter.Name = "Counter"
Counter.Parent = Leaderstats
table.insert(Players, player)
end)
while true do
task.wait(60)
for _, player in ipairs(Players) do
player.leaderstats.Counter.Value += 1
end
end
You don't need to check if "Counter" or "leaderstats" exist as they are created before the player is being inserted into the table.
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()
I'm trying to make a cutscene sorta thing for a dorr in roblox studio. My solution was to set up a collision detector on the door which would then make a gui template and set its parent to the playergui component.
I did this using the code
local afterIntoTransform = script.Parent.Parent.DoorUnion.Position.Z -6
local afterOutwardsTransform = script.Parent.Parent.DoorUnion.Position.Z + 6
local debounce = false
local function executeFadeSceneAndTpPlayer(player)
local fadeScene = Instance.new("ScreenGui")
local fadeSceneFrame = Instance.new("Frame")
fadeScene.Name = "fadeScene"
fadeSceneFrame.Name = "fadeFrame"
fadeSceneFrame.Size = UDim2.new(1,0,1,0)
fadeSceneFrame.Parent = fadeScene
fadeSceneFrame.BorderSizePixel = 0
fadeSceneFrame.BackgroundColor3 = Color3.new(1, 1, 1)
fadeSceneFrame.BackgroundTransparency = 1
print(game.Players:GetPlayerFromCharacter(player).Name)
fadeScene.Parent = game.Players:GetPlayerFromCharacter(player).PlayerGui
for i = 0, 20, 1 do
fadeSceneFrame.BackgroundTransparency -= 0.05
wait(0.01)
end
player.HumanoidRootPart.Position = Vector3.new(player.HumanoidRootPart.Position.X, player.HumanoidRootPart.Position.Y, afterOutwardsTransform)
for i = 0, 20, 1 do
fadeSceneFrame.BackgroundTransparency += 0.05
wait(0.01)
end
fadeScene:Destroy()
end
script.Parent.Touched:Connect(function(hit)
if not debounce then
debounce = true
executeFadeSceneAndTpPlayer(hit.Parent)
wait(0.2)
debounce = false
end
end)
It tells me: Attempted to index nil with name on line 15.
It works sometimes and sometimes doesnt but recently Ive noticed a trend that I can walk into the door then out again and then it breaks. I haven't coded in a while so I'm a little rusty but I hope I can get some help.
You are running into the same issue as this person : Roblox - attempt to index nil with 'leaderstats'
You are not accounting for the fact that the Touched event fires for every single part that touches it, and some of those parts might not belong to a player.
You can protect against this error by making sure that the object belongs to a player before you call executeFadeSceneAndTpPlayer()
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if not plr then
return
end
if not debounce then
debounce = true
executeFadeSceneAndTpPlayer(hit.Parent)
wait(0.2)
debounce = false
end
end)