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

Why is this not changing the value on touch?

Asked by 5 years ago

I have it so that when you touch a part, it finds a value within the character and adds a preset amount that's found inside another value in the part.

01debounce = false
02script.Parent.Touched:connect(function(hit)
03    if debounce == false
04    then
05        debounce = true
06    if hit.Parent:FindFirstChild("Humanoid") then
07        if hit.Parent:FindFirstChild("money") then
08 
09            local money = hit.Parent:WaitForChild("money")
10            local amount = script.Parent:WaitForChild("howmuchmoney").Value
11            money.Value = money.Value + amount
12            wait(.5)
13            debounce = false
14 
15            end
16        end
17    end
18end)
0
Is this running in a server script and are there any errors in console? IStarConquestI 414 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Make sure that "money" is stored in the character and not the player. Also, add print()s every time you are unsure if the code reaches that point to see what went wrong. If the code reaches past line 6 but not past line 7, we know that hit.Parent:FindFIrstChild("money") is returning nil, so there's something wrong with that. This is a trivial question for you to debug yourself, just remember to use print statements and find your mistakes.

01debounce = false
02script.Parent.Touched:connect(function(hit)
03    if debounce == false then
04            debounce = true
05        print("First statement pass")
06        if hit.Parent:FindFirstChild("Humanoid") then
07            print("Second statement pass")
08                if hit.Parent:FindFirstChild("money") then
09                print("Third statement pass")
10                local money = hit.Parent:WaitForChild("money")
11                local amount = script.Parent:WaitForChild("howmuchmoney").Value
12                money.Value = money.Value + amount
13                wait(.5)
14                debounce = false
15                print("If it has reached this point, then it's working.")
16            end
17        end
18    end
19end)
Ad

Answer this question