Accessing player stats from server script error

I am trying to set up a barebones fighting system. I have a function on a serverscript to handle the attack function:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local attackEvent = Instance.new("RemoteEvent")
attackEvent.Name = "AttackEvent"
attackEvent.Parent = ReplicatedStorage

local function onAttackEvent(player,target, damage)
	local damageOutput
	local damageDealt
	local playerStats = player:WaitForChild("Stats")

	damageOutput = player.Stats.Strength.Value
	damageDealt = (damageOutput - target.Stats.Defense.Value)
	print(damageDealt," dmg applied!")
	
end

attackEvent.OnServerEvent:Connect(onAttackEvent)

I have a button that fires the attack event, this is the localscript in the button:

local button = script.Parent

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local attackEvent = ReplicatedStorage:WaitForChild("AttackEvent")


local function leftClick()
	print("ATTACK!")
	--send remoteEvent to CharScript
	attackEvent:FireServer(game.Workspace.R15)
end

local function rightClick()
	print("Right mouse click")
end

button.MouseButton1Click:Connect(leftClick)
button.MouseButton2Click:Connect(rightClick)

And I have a localscript which creates the different stats for the player:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- STATS
local statsFolder = Instance.new("Folder")
statsFolder.Parent = player
statsFolder.Name = "Stats"

local strength = Instance.new("IntValue")
strength.Parent = statsFolder
strength.Value = 10
strength.Name = "Strength"

local agility = Instance.new("IntValue")
agility.Parent = statsFolder
agility.Value = 10
agility.Name = "Agility"

local defense = Instance.new("IntValue")
defense.Parent = statsFolder
defense.Value = 10
defense.Name = "Defense"

local speed = Instance.new("IntValue")
speed.Parent = statsFolder
speed.Value = 10
speed.Name = "Speed"

local hp = Instance.new("IntValue")
hp.Parent = statsFolder
hp.Value = 10
hp.Name = "HP"

local mana = Instance.new("IntValue")
mana.Parent = statsFolder
mana.Value = 10
mana.Name = "Mana"

local level = Instance.new("IntValue")
level.Parent = statsFolder
level.Value = 1
level.Name = "Level"

local xp = Instance.new("IntValue")
xp.Parent = statsFolder
xp.Value = 0
xp.Name = "XP"

local gold = Instance.new("IntValue")
gold.Parent = statsFolder
gold.Value = 0
gold.Name = "Gold"

local gems = Instance.new("IntValue")
gems.Parent = statsFolder
gems.Value = 0
gems.Name = "Gems"

local weapon = Instance.new("IntValue")
weapon.Parent = statsFolder
weapon.Value = "sword"
weapon.Name = "Weapon"

The problem I am facing is that in the attack function (the first script up there) can’t seem to access the stats of the player.

local playerStats = player:WaitForChild("Stats")

runs an infinite yield. The stats folder does not exist according to this script, even though the stats folder is present under the player. I need to be able to access the player’s stats from this function, but I don’t know how to do that because apparently I’m doing something wrong here. Any help would be appreciated.

1 Like

Well its pretty simple, all the stats and folders are only clientside… So the ServerSided script cant access it because it doesnt exist :confused:

I figured it was something like that. What should I do here? Can I maybe replicate the stats on the server side?

The problem is that you are trying to access the Stats folder before it is created. You can fix this by making sure to create the Stats folder before trying to access it. The best way to do this would be to move the code that creates the Stats folder into the same function as the one that accesses the Stats folder. This way, the Stats folder will always be created before the function tries to access it.

Don’t worry, this is easy. Just place it in a serverscript in scriptservice

local function leaderboardSetup(player)
--put your stats code here
end

Players.PlayerAdded:Connect(leaderboardSetup)

And as @Bazzive said, localscripts will create stuff only for the player (client) not for the server. Use playeradded to get players.

That’s not possible since the player does not have access to the button straight away as the Stats script runs. It’s about the client-sided problem.

Pretty simple fix :slight_smile:
Just make a server script instead!

-- STATS

game:GetService("Players").PlayerAdded:Connect(function(player)
local statsFolder = Instance.new("Folder")
statsFolder.Parent = player
statsFolder.Name = "Stats"

local strength = Instance.new("IntValue")
strength.Parent = statsFolder
strength.Value = 10
strength.Name = "Strength"

local agility = Instance.new("IntValue")
agility.Parent = statsFolder
agility.Value = 10
agility.Name = "Agility"

local defense = Instance.new("IntValue")
defense.Parent = statsFolder
defense.Value = 10
defense.Name = "Defense"

local speed = Instance.new("IntValue")
speed.Parent = statsFolder
speed.Value = 10
speed.Name = "Speed"

local hp = Instance.new("IntValue")
hp.Parent = statsFolder
hp.Value = 10
hp.Name = "HP"

local mana = Instance.new("IntValue")
mana.Parent = statsFolder
mana.Value = 10
mana.Name = "Mana"

local level = Instance.new("IntValue")
level.Parent = statsFolder
level.Value = 1
level.Name = "Level"

local xp = Instance.new("IntValue")
xp.Parent = statsFolder
xp.Value = 0
xp.Name = "XP"

local gold = Instance.new("IntValue")
gold.Parent = statsFolder
gold.Value = 0
gold.Name = "Gold"

local gems = Instance.new("IntValue")
gems.Parent = statsFolder
gems.Value = 0
gems.Name = "Gems"

local weapon = Instance.new("IntValue")
weapon.Parent = statsFolder
weapon.Value = "sword"
weapon.Name = "Weapon"
end)

This makes it so that there are stats in the player folder, but the server can access it way easier

1 Like

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