I have a game that uses FindFirstChild()
to give the player money when they touch a brick. It results with an error but still works. My code is something like this (not exact cause I am on mobile and doing it off my head):
local denounce = 0 script.Parent.Touched:connect(function(player) If debounce == 0 then debounce = 1 local plr = player:GetPlayerFromCharacter() local stats = plr:FindFirstChild('leaderstats') local value = stats:FindFirstChild('Money') value.Value = value.Value + 100 wait(5) debounce = 0 end
Thanks, JailBreaker_13
Hello. There are quite some problems and recommendations I will give you with your script.
Problems:
You did not use the argument of GetPlayerFromCharacter()
.
You forgot an end.
You did not check if the part hit was a player.
Solutions:
Make the argument of GetPlayerFromCharacter()
"hit.Parent".
Add an end.
Make an if statement checking if the player is what touched the part.
Recommendations:
Use connect
with an uppercase "C" as connect
is deprecated. It is never a good idea to use deprecated functions.
Rename the parameter for the touched event to "hit" as it makes more sense.
You don't need to use FindFirstChild()
since all players will have a leaderstats and money value.
Use a true
and false
based debounce instead of a number based one.
Fixed Script:
local debounce = false script.Parent.Touched:Connect(function(hit) local plr = player:GetPlayerFromCharacter(hit.Parent) if not debounce and plr then debounce = true local stats = plr.leaderstats local value = stats.Money value.Value = value.Value + 100 wait(5) debounce = false end end