So im now making a fnaf security breach tycoon and i cant figure out the way to make it so when a part hits the destroy thing it gives you tokens
local Freddy = game.Workspace:GetChildren("Freddy")
local part = script.Parent
local tokens = game:WaitForChild("leaderstats"):WaitForChild("Tokens")
part.Touched:Connect(function()
tokens.Value += 2
Freddy:Destroy(Freddy)
end)
Thanks for helping me
ive ran into this multiple times i cant figure this out though
local tokens = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Tokens")
Also I think Freddy:Destroy(Freddy) will error. Change it to Freddy:Destroy(). If it still errors, then you have to change your local Freddy variable to this:
local Freddy = game.Workspace:WaitForChild("Freddy")
What I do to fix this is wait a few seconds to load the script, here we go.
task.wait(5)
local Freddy = game.Workspace:GetChildren("Freddy")
local part = script.Parent
local tokens = game.Players.LocalPlayer:WaitForChild("leaderstats"):WaitForChild("Tokens")
part.Touched:Connect(function()
tokens.Value += 2
Freddy:Destroy(Freddy)
end)
Script wont work until 5 seconds has passed it gives it time so it lets roblox load everything in
I saw you did “game:WaitForChild(“leaderstats”)” You need to get the player. There is a way to do what you want to do on the client BUT if you add datastore it wont save, you need to use remote event or remote function to connect to the server AND a better touched detection. You need to look over the devforum to use Remotes and a better touch detections.
Okay. The reason you are getting this error is because “leaderstats” does not exist when you just call game, it is in the player object. Basically, you just want to get the player from the touch event, and then get the leaderstats folder that is in the player object and increase the specific value in leaderstats folder.
Use the following code below:
local Freddy = game.Workspace:WaitForChild("Freddy")
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then -- if whatever touched the part has a humanoid
local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- gets the player from the character
if player then -- double checks if the player exist
player:FindFirstChild("leaderstats").Tokens += 2 -- adds the value
Freddy:Destroy() --destroys that
end
end
end)
local part = script.Parent
Part.Touched:Connect(function(Hit)
if Hit.Parent ~= nil then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
local leaderstats = Player:WaitForChild("leaderstats")
local Tokens = leaderstats:WaitForChild("Tokens")
Tokens.Value += 2
end
end
end)
I shouldn’t of really gave u the full solution but you would of never of got all of this if your a starter, just put this in a server script and it should fix your issue, I havent tested so if anything doesnt work please let me know
Ok uhh, That didnt work and i dont know if your looking at what im looking at i need it for a tycoon and im thinking that i should put them in two diffrent scripts
Wait, this error does not seem relevant to the code that I have given. There is no :WaitForChild() on the 4th line of the code I gave above. Who’s code have you used right now?