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

Is there a way to make a touched event work in a local script?

So, I have a script that uses local player, and a touched function in it. It just flat out doesn't work. I know I can use remote events, but is there a different way to do it? Here's my code. Could there also be a way to access the player, from the character?

script.Parent.Touched:Connect(function(part2)
	local Player = game.Players:GetPlayerFromCharacter(part2.Parent) -- Checks and returns if the part associates to a player
	if Player then
		wait()
			print ("Touched")
			game.Players.LocalPlayer.Data.PistolAmmo.Value = game.Players.LocalPlayer.Data.PistolAmmo.Value + 5
		end
end)

1 answer

✓ Accepted answer
script.Parent.Touched:Connect(function(part2)
    -- if something touches script.Parent, the function runs
    if game.Players:GetPlayerFromCharacter(part2.Parent) then
	   -- if a player touched the part, this runs
	   local Player = game.Players:GetPlayerFromCharacter(part2.Parent)
	    -- takes player from character
        wait() -- waits
            print ("Touched") -- prints "Touched" into output
            Player.Data.PistolAmmo.Value = Player.Data.PistolAmmo.Value + 5
	    -- takes the Player.Data.PistoAmmo.Value and adds 5 to it
        end
end)

5 comments

0

Oh, you can? Thank you!

0

Yup.

0

Please explain the Code.

0

Ok.

0

You didn't have to explain my code, lol. Maybe just explain the GetPlayerFromCharacter.

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