Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

OnTouched nil problem?

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)

2 comments

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.

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

1 answer

✓ Accepted answer

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)

5 comments

2

I'm not sure that line 13 would even run, since there's nothing to stop the while loop above it.

0

Perci1: You're right. The connection to TouchEnded would have to be before the loop

0

Thanks, missed that.

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 :/

0

Add a debounce(;

This discussion is preserved as it was. New answers, comments, and votes are closed.