Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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)
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 Sulu710 142 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
0
Thank you! Everbyte 6 — 4y
0
You're welcome. arondevs 65 — 4y
Ad

Answer this question