Finding player in serverscript help

So when I find a player in the servescript and change one of their values like leaderstats, it changes for all of the players?
Start of the script=

game.Players.PlayerAdded:Connect(function(plr)
	local rp = game:GetService("ReplicatedStorage")
	local event = rp.Rebirth
	local player  = plr.Character


	local othersutff = plr:WaitForChild("OtherStuff")
	local rebirth = othersutff.rebirths
	local rebirthcost = plr.OtherStuff.rebirthcost
	local leaderstats = plr:WaitForChild("leaderstats")
	local cash = leaderstats.Cash
	local wood =leaderstats.Wood

2 Likes
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
-- Follow the same proccess for making other values

Now all you need to do is whenever the player does something, like killing another player. You need to get the player instance and just do something like:

-- Specified the player instance
plr.leaderstats.Cash.Value = 100

Also it would be helpful to see your full script.

1 Like

Just to make sure all i need to do is plr.leaderstats.cash.Value += instad of cash.value +=?

Here is the full code:

game.Players.PlayerAdded:Connect(function(plr)
local rp = game:GetService(“ReplicatedStorage”)
local event = rp.Rebirth
local player = plr.Character

local othersutff = plr:WaitForChild("OtherStuff")
local rebirth = othersutff.rebirths
local rebirthcost = plr.OtherStuff.rebirthcost
local leaderstats = plr:WaitForChild("leaderstats")
local cash = leaderstats.Cash
local wood =leaderstats.Wood



event.OnServerEvent:Connect(function()

	print(cash.Value)
	print(rebirthcost.Value)
	if tonumber(cash.Value) >= tonumber(rebirthcost.Value) then   
		rebirthcost.Value *= 2
		rebirth.Value += 0.1
		math.ceil(rebirth.Value)
		math.ceil(rebirthcost.Value)
		cash.Value = 0
		wood.Value = 0
		print(rebirth.Value)
	else
		print("not enough money")
	end
end)


rp.Convert.OnServerEvent:Connect(function()
	if plr.axes.twxCash.Value == true then
		cash.Value += wood.Value * rebirth.Value * 2
		wood.Value = 0
	else
		cash.Value += wood.Value * rebirth.Value
		wood.Value = 0
	end
end)


local RS = game:GetService("ReplicatedStorage")
local AddWoodEvent = RS.AddWood
local DamageValue = 0

local function updatedamg(idk: NumberValue) 
	local Character = plr.Character or plr.CharacterAdded:Wait()
	local Axe = plr.Character:WaitForChild("Axe")
	local idk =	Axe:FindFirstChild("damage").Value
	print(idk)
	return idk
end

AddWoodEvent.OnServerEvent:Connect(function(Player)
	DamageValue = updatedamg(DamageValue)
	if not DamageValue then warn("Axe has no damage value") return end

	local PlayerAxes = Player:WaitForChild("axes", 5)
	if not PlayerAxes then warn("Player has no axes") return end

	local twxWood = PlayerAxes:FindFirstChild("twxWood", 5)

	print(DamageValue)
	wood.Value += DamageValue * (twxWood.Value and 2 or 1) 
	DamageValue = 0
end)


rp.Cleartools.OnServerEvent:Connect(function()
	for _, child in pairs(plr.Character:GetChildren()) do
		if child:IsA("Tool") then 
			child:Destroy() 
		end
	end
	for _, child in pairs(plr.Backpack:GetChildren()) do
		if child:IsA("Tool") then 
			child:Destroy()
		end
	end
	task.wait()
end)

rp.Areas.Forest.OnServerEvent:Connect(function(plr)
	if cash.Value >= 5000 then
		plr.axes.Forest.Value = true 
		cash.Value -= 500
		rp.Areas.sendForest:FireClient(plr)
	else
		return
	end
end)

rp.Areas.Farm.OnServerEvent:Connect(function(plr)
	if cash.Value >= 50000 then
		plr.axes.Farm.Value = true 
		cash.Value -= 50000
		rp.Areas.sendFarm:FireClient(plr)
	else
		return
	end
end)

end)

1 Like

Why dont you just put that part inside the same script where you defined the leaderstats?

1 Like

I see your problem now. On some of your serverevents for your remote event you aren’t using the player instance that is returned in the parameters.

local rebirthcost = plr.OtherStuff.rebirthcost
local leaderstats = plr:WaitForChild("leaderstats")

This isn’t the same player instance as the player who has done the action that has fired the remote event to the server. Also, it’s probably because you have these server events inside of that playeradded event. A playeradded event is supposed to only run when the player is added. Put those remote events outside of the playeradded event.

Even in the server events where you do put in the plr instance into the parameters. You don’t use them, you instead use the variables you made. Here’s an example of how you would fix one of your things.

rp.Areas.Forest.OnServerEvent:Connect(function(plr)
	if plr.leaderstats.cash.Value >= 5000 then
		plr.axes.Forest.Value = true 
		plr.Cash.Value -= 500
		rp.Areas.sendForest:FireClient(plr)
	else
		return
	end
end)

And do this outside of your playeradded event so you don’t accidently muddle up the player instances.

3 Likes

If i put them out of the playeradded event then I will not get the plr and is the whole reason i added player added event is to find the player in a serverscript.

Yes you will, literally look you gave yourself the player instance by accident lol.

rp.Areas.Forest.OnServerEvent:Connect(function(plr) -- I didn't even put this in you did

But you didn’t even use it ;-;

1 Like

Put it outside the playeradded event and watch everything work seamlessly. (If you also do it correctly of course)

1 Like

I canty put them out of the event because if ido then they do not know what plr is?

local leaderstats =       plr   :WaitForChild("leaderstats")
1 Like
rp.Areas.Forest.OnServerEvent:Connect(function(plr)
local leaderstats = plr.leaderstats
end)

In that code above, you can see I’m getting the specific player instance. When you fire a remote event, it returns the player instance that fired it. So that’s how you can use it and you can figure out which player did it Booooom.

2 Likes

Thanks, I have one more question, so whenever the player clciks on the button it says the player does not own forest but gets teleports to it? This is on players who i have tested to try to bug fix it on so it might be because the players are bugged or idk?

local TeleportPart1 = workspace:WaitForChild("ring2").Part


script.Parent.MouseButton1Click:Connect(function(plr)
	local w = game.Players.LocalPlayer:WaitForChild("axes").Forest
	if w.Value == true then
		game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = TeleportPart1.CFrame + Vector3.new(0, 5, 0)
	else
		game.ReplicatedStorage.Areas.Forest:FireServer(plr)
	end   
end)
1 Like
game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = TeleportPart1.CFrame + Vector3.new(0, 5, 0)

Not sure what you mean but I don’t think this line of code would run because you are using the + operator and a Vector3 on a cframe. If you’re getting the cframe of the HRP then you should use cframes only. Like this

game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = TeleportPart1.CFrame * CFrame.new(0, 5, 0)
3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.