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

How to make a script only affect the local player parent?

Asked by
zomspi 541 Moderation Voter
4 years ago

I have a script which adds a leaderstat value when a click detector is clicked, except it changes the leaderstat value for all players in the game instead of the player who clicked it?

local MS = game:GetService("MarketplaceService")
local VIPID = 8110615
local clickDetector = script.Parent.ClickDetector
local RS = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(plr)
    if MS:UserOwnsGamePassAsync(plr.UserId,VIPID) then

        clickDetector.MouseClick:Connect(function()
            plr.leaderstats.Potatoes.Value = plr.leaderstats.Potatoes.Value + 1
        end)
    end
end)

(Ignore the gamepass)

4 answers

Log in to vote
1
Answered by 4 years ago

Well, I wouldn't recommend putting that MouseClick event inside the PlayerAdded event... You can easily grab the player who clicked in the MouseClick event because MouseClick returns Player! Read about it here

With that being said, here's your fixed script;

local MS = game:GetService("MarketplaceService")
local VIPID = 8110615
local clickDetector = script.Parent.ClickDetector
local RS = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(plr)
    if MS:UserOwnsGamePassAsync(plr.UserId,VIPID) then
        --not sure what you want here
    end
end)

clickDetector.MouseClick:Connect(function(plr)
    plr.leaderstats.Potatoes.Value = plr.leaderstats.Potatoes.Value + 1
end)
Ad
Log in to vote
0
Answered by 4 years ago

I would say handle the click on the client, then fire a RemoteEvent. Change the player's leaderstats on the server then.

Log in to vote
0
Answered by 4 years ago

Use a Local Script

0
L0L greatneil80 2647 — 4y
Log in to vote
0
Answered by
zomspi 541 Moderation Voter
4 years ago

@GeneratedScript

I tried to do that and this is my script, but when I try to write plr it underlines it red?

game.Players.PlayerAdded:Connect(function(plr)

script.Parent.OnServerEvent:Connect(function()
    plr -- Here
end)
end)

Answer this question