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.