Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

FindFirstChild() results with an error but no bug?

Asked by 4 years ago
Edited by youtubemasterWOW 4 years ago

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

0
Why did you close my question for not being constructive? Everyone has their own issues and problems and sometimes they don't know where to start. You blatantly removed my question for "not being constructive enough" which left me with a negative 5 reputation. Everyone starts from somewhere and YOU are not acknowledging that. Dev_Zephyr -5 — 3y
0
I'm sorry if you are upset, but I was just doing my job, thank you for your feedback, I will take this into consideration when moderating. It was not just me who thought it was not constructive, there are many others that voted on it as well. JailBreaker_13 350 — 3y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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
0
Thanks for your help. (Some errors were because i am on mobile.) JailBreaker_13 350 — 4y
0
Alright and no problem. youtubemasterWOW 2741 — 4y
0
This did not work. JailBreaker_13 350 — 3y
Ad

Answer this question