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

OnTouched nil problem?

Asked by
Paldi 109
9 years ago

I want it so when the player is not touching the brick anymore, it stops removing the value at line 9

function onTouched(hit)
    hit =  game:GetService("Players").LocalPlayer
shower = true
while shower == true do
    wait(1)
    if hit ~= nil then
            if hit.PlayerGui.UI.Sections.Fatigue.Fatigue.Value >= 5 then
                hit.PlayerGui.UI.Sections.Fatigue.Fatigue.Value = hit.PlayerGui.UI.Sections.Fatigue.Fatigue.Value-5
            end
    elseif hit == nil then
        shower = false
    end
end
end




script.Parent.Touched:connect(onTouched)

0
What do you want to happen to this ? What do you want to happen when the player touches the brick? If you tell me that I may be able to help you. Teeter11 281 — 9y
0
when the player touch the brick, if the fatigue.value is >= 5 then it remove 5 from it this part is working but it keep doing it even if i dont touch it anymore Paldi 109 — 9y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

To do what you want to do, you need to use the TouchEnded event.

The TouchEnded Event is similiar to the Touched Event, but rather than the initiation of the touch.. it's when you stop touching it(:


Also, on line 2, you're using LocalPlayer to get the player. This won't work as expected because the code should be a Server-script, and LocalPlayer only works in LocalScripts.

So, an alternative is to use the GetPlayerFromCharacter function. Use hit.Parent for the arguments as that is the potential character.


script.Parent.Touched:connect(function(hit)
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
    local shower = true
    if plr then
        local val = plr.PlayerGui.UI.Sections.Fatigue.Fatigue
        --Make a thread so that code beyond the while loop can run(:
        coroutine.wrap(function()
            while shower do
                 wait(1)
                if val.Value >= 5 then
                    val.Value = val.Value - 5
                end
            end
        end)()
    end
    script.Parent.TouchEnded:connect(function()
        shower = false
    end)
end)
2
I'm not sure that line 13 would even run, since there's nothing to stop the while loop above it. Perci1 4988 — 9y
0
Perci1: You're right. The connection to TouchEnded would have to be before the loop BlueTaslem 18071 — 9y
0
Thanks, missed that. Goulstem 8144 — 9y
0
now it kinda works but when i touch the brick it just pu the value to 0 immediately because of the other bodyparts that touch the brick :/ Paldi 109 — 9y
0
Add a debounce(; Goulstem 8144 — 9y
Ad

Answer this question