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

OnTouch won't work?

So, I've been having problems getting an onTouch script to work in a server. In test mode it works fine, but on an actual server it fails to work. At first I thought it was the value that wasn't responding, but then I added a print to the script to make sure that the onTouch was actually firing, and, to my surprise, it was actually the script itself that was failing to fire when the brick was touched. Here's the script:

function onTouch(t)
	print("is this working?")
		game.Players.LocalPlayer.PlayerGui.HUD.TextT.Trigger.Value = "enemy"
end

script.Parent.Touched:connect(onTouch)

It's a very simple script, which confuses me as to why it isn't working. All help is appreciated.

1 answer

✓ Accepted answer

Nice attempt. For TouchedEvents, they need to be server-sided scripts (A normal script), usually located inside the part that's touched. Since it's server-sided, you can't use LocalPlayer. Now, you may be wondering, how do I get the player? Well, you can use the :GetPlayerFromCharacter() Method! Here's your code fixed up:

script.Parent.Touched:connect(function(hit)
	if game.Players:GetPlayerFromCharacter(hit.Parent) then -- Checks if it's a player
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		player.PlayerGui.HUD.TextT.Trigger.Value = "enemy" -- If the game is FilteringEnabled, you'll need to use RemoteEvents for this line
	end
end)

Hope I helped! If you have any questions, feel free to comment!

2 comments

0

Thank you! This will be good to know for future scripts, too!

0

No problem!

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