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

How to fix server side damage script?

Im trying to make it so when you punch you deal damage, i have the local script i just need help with the server script to send the damage to the server but i have no idea on what im doing.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Punch = ReplicatedStorage:WaitForChild("Punch")
local player = game.Players.LocalPlayer
local str = player.leaderstats.strength

Punch.OnServerEvent:Connect(function(player)
	player.Parent.Humanoid:TakeDamage(str)
end)


This is what i have so far, im getting the error "attempt to index nil with 'leaderstats' i want to make it so the more strength you have that's the amount of damage you do so if u earned 100 strength you do 100 damage, etc.

2 comments

0

i can't really help with the index nil with leaderstats thing but i do think i see something else, on local str = player.leaderstats.strength you should add .Value to the end of it so it can be assigned to the value of strength and not just the variable strength

0

I got the same error, is there another way i can get the players stats?

2 answers

✓ Accepted answer

If the script you put is the server script, you can't access LocalPlayer from a serverside script. Has to be called from a local script only. Alternative way of getting the player is game.PlayerAdded

Thanks for the help, i've gotten it now heres the updated script

game.Players.PlayerAdded:Connect(function(player)

end)

game.ReplicatedStorage.Punch.OnServerEvent:Connect(function(player, humanoid)
	local str = player.leaderstats.strength.Value
	humanoid:TakeDamage(str)
end)

1 comment

0

You should insert the "OnServerEvent" function inside the "PlayerAdded" function as well

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