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

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Geobloxia 251 Moderation Voter
4 years ago
Edited 4 years ago
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)
0
Oh, you can? Thank you! zandefear4 90 — 4y
0
Yup. Geobloxia 251 — 4y
0
Please explain the Code. Ziffixture 6913 — 4y
0
Ok. Geobloxia 251 — 4y
0
You didn't have to explain my code, lol. Maybe just explain the GetPlayerFromCharacter. zandefear4 90 — 4y
Ad

Answer this question