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

OnTouch won't work?

Asked by 9 years ago

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

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

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!

0
Thank you! This will be good to know for future scripts, too! ZeMeNbUiLdEr 104 — 9y
0
No problem! Shawnyg 4330 — 9y
Ad

Answer this question