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

How do I make it read the value of Players.LocalPlayer.Leaderstats.(Anything).Value?

I've been trying to have a animation punch that at the start does 2 damage but times it by the leaderboard stat called "Strength" but it keeps saying "ServerScriptService.Punch Damage:11:Attempt to index field 'LocalPlayer' (A nil value). It also makes a sound upon punch.

This script is in ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local sound = game.ServerStorage.Punch:Clone()
		sound.Parent = char:WaitForChild("Head")
	end)
end)


game.ReplicatedStorage.Punch.OnServerEvent:Connect(function(player, humanoid)
	if humanoid.Health >= 10 then
		humanoid.Health = humanoid.Health - 2 * game.Players.LocalPlayer.leaderstats.strength.Value
	elseif humanoid.Health < 2 then
		humanoid.health = 0
	end
	
	player.Character.Head.Punch:Play()
end)

1 comment

0

You can only use "LocalPlayer" from a client side local script, as it references the player that is on the client in which the local script is being run

1 answer

✓ Accepted answer

You can't mention LocalPlayer in a ServerScript, as it is not client sided. Insead, use this:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local sound = game.ServerStorage.Punch:Clone()
        sound.Parent = char:WaitForChild("Head")
    end)
end)


game.ReplicatedStorage.Punch.OnServerEvent:Connect(function(player, humanoid)
    if humanoid.Health >= 10 then
        humanoid.Health = humanoid.Health - 2 * player.leaderstats.strength.Value
    elseif humanoid.Health < 2 then
        humanoid.health = 0
    end

    player.Character.Head.Punch:Play()
end)

2 comments

0

Thank you!

0

You're welcome.

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