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 my script give leaderstat value only to player who touched the part, not all server?

Explaination: Ok so i made a portals and scripts what teleporting player to next stage and giving to player leaderstats +1 stage completed. So, we have tested that system with my friend, and figured that script gives +1 stage completed to all players on server. So im wanna change it to make give +1 stage only for those players, who touched portal script.

Scripts:

Local StarterPack scripts(they are almost same, just part what player touching changed):

local touchName = "Stages"
	local rs = game:GetService("ReplicatedStorage")
	 
	local touchEvent = rs:WaitForChild('TouchEvent')
	local portal = workspace.FirstStage.FrostPartal.PortalPart
	local Debounce = false
	 
	portal.Touched:Connect(function()
		    if(Debounce == false) then
			        Debounce = true 
			        touchEvent:FireServer() 
			        wait(1) 
			        Debounce = false
end)

Server Leaderstats Script:

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	
	local start = Instance.new("IntValue")
	start.Name = "Stages"
	start.Value = 0
	start.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

local touchName = "Stages"
local rs = game:GetService("ReplicatedStorage")

local touchEvent = rs:WaitForChild('TouchEvent')



touchEvent.OnServerEvent:Connect(function(plr)

	plr.leaderstats[touchName].Value = plr.leaderstats[touchName].Value +1
	
	wait(1)
	
	
	

end)

1 answer

✓ Accepted answer

Alright so this isn't guaranteed to help you since I've only been scripting for almost 2 months but I'll try to help.

Don't put the touched event in a LocalScript. I once did a touch and teleporting event there and it broke my whole game. Do it in a ServerScript instead. And as you're doing that, there's no need to do a RemoteEvent.

That's probably the main problem so try getting rid of your LocalScript and do this Script in ServerScriptService

game.Players.PlayerAdded:Connect(function(plr) --Making a PlayerAdded function and event
	local leaderstats = Instance.new("Folder") --Making a leaderstats
	leaderstats.Parent = plr --Parenting the leaderstats to the player
	leaderstats.Name = "leaderstats" --Changing its name to leaderstats
	local start = Instance.new("IntValue") --Adding the stage values
	start.Name = "Stages" --Naming it Stages
	start.Parent = leaderstats --Parenting it to leaderstats
end)

local db = false --Adding a debounce

local portal = workspace.FirstStage.FrostPartal.PortalPart --Defining the portal

portal.Touched:Connect(function(hit) --Touched function
	if hit.Parent:FindFirstChild("Humanoid") and not db then --Checking if a player touched it and that debounce is false
		db = true --Making debounce true
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Getting the player that touched it
		plr.leaderstats.Stages.Value = plr.leaderstats.Stages.Value + 1 --Adding value
		wait(10) --The cooldown for the debounce, you can make this as much as you want
		db = false --Making debounce false
	end
end)

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