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

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

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

✓ Accepted answer

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)

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

@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)

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