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

How do i fix this counter brick?

Asked by 4 years ago

Im attempting to make a brick that when touched will identify what number toucher you were. Specifically, when someone touches the brick, it should detect how many times its been touched, and put that number in a numbervalue inside of the character. So the first person to touch it should get a value of one, second should get 2, so on so fourth.

I made the script:

local touches = script.Parent.touches.Value -- number value counting the times its been touched
function onTouched(hit)

if hit.Parent:findFirstChild("Humanoid") then
    touches = touches + 1 -- add a number to touches
    wait(1)
    hit.Parent.pednum.num.Value = touches -- update the characters number
end
end

script.Parent.Touched:connect(onTouched)

However, when touching the brick, the "touches" value does not increase at all, and the players "pednum" value goes up by a random amount. I'm not even sure where to go about this. Im running a test server for this in edit mode.

1 answer

Log in to vote
0
Answered by 4 years ago

Line 04, all of the "f"s in FindFirstChild need to be capitalized. Also, try removing the .Value from the touches variable. I don't know if it's just me, but it's always given me problems. Then on line 05 just do touches.Value = touches.Value + 1. Also, I'd suspect that the pednum isn't going up a random amount, it's just firing multiple times when you touch it. Add a debounce to your script to make sure it only runs once.

local touches = script.Parent.touches -- number value counting the times its been touched
local debounce = false

function onTouched(hit)

if hit.Parent:findFirstChild("Humanoid") and debounce == false then

debounce = true
touches.Value = touches.Value + 1 -- add a number to touches
hit.Parent.pednum.num.Value = touches -- update the characters number
debounce = false
wait(1)
end
end

script.Parent.Touched:connect(onTouched)

Try this and tell me if it works. I am still new to scripting, so it may not work on the first go around but we'll figure it out together.

Ad

Answer this question