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

When tool is activated it gives everyone Strength instead of the player that clicked it why?

Asked by 4 years ago

I don't understand much about remote events but this is my code,

local player = game.Players.LocalPlayer
local lift = game.ReplicatedStorage.Lift
local sell = game.ReplicatedStorage.Sell

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

    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = player
    leaderstats.Name = "leaderstats"

    local Strength = Instance.new("NumberValue")
    Strength.Name = "Strength"
    Strength.Parent = leaderstats

    local Cash = Instance.new("NumberValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    lift.OnServerEvent:Connect(function(player)
        Strength.Value = Strength.Value + 1
    end)

    sell.OnServerEvent:Connect(function(player)
        Cash.Value = Strength.Value + Cash.Value
        Strength.Value = 0
    end)

    player.CharacterAppearanceLoaded:Connect(function(character)
        local humaniod = character.Humanoid
        humaniod:WaitForChild("BodyDepthScale").Value = .5 + (Strength.Value / 100)
        humaniod:WaitForChild("BodyHeightScale").Value = .5 + (Strength.Value / 100)
        humaniod:WaitForChild("BodyWidthScale").Value = .5 + (Strength.Value / 100)
        humaniod:WaitForChild("HeadScale").Value = .5 + (Strength.Value / 100)
        humaniod.WalkSpeed = 16 + (Strength.Value / 10)
        humaniod.MaxHealth = 100 + (Strength.Value / 10)

        Strength:GetPropertyChangedSignal("Value"):Connect(function()
            humaniod:WaitForChild("BodyDepthScale").Value = .5 + (Strength.Value / 100)
            humaniod:WaitForChild("BodyHeightScale").Value = .5 + (Strength.Value / 100)
            humaniod:WaitForChild("BodyWidthScale").Value = .5 + (Strength.Value / 100)
            humaniod:WaitForChild("HeadScale").Value = .5 + (Strength.Value / 100)
            humaniod.WalkSpeed = 16 + (Strength.Value / 10)
            humaniod.MaxHealth = 100 + (Strength.Value / 10)
        end)
    end)
end)

2 answers

Log in to vote
0
Answered by 4 years ago

Because, the RemoteEvent is firing the whole server for each player, which gives everyone strength. To fix this, do it through the tool with a ServerScript, and you won't have to use RemoteEvents. Just a ServerScript.

You can keep the data for the strength, cash, and leaderstats. Just move everything else to a ServerScript inside the tool

Example:

ServerScript inside Tool

script.Parent.Activated:Connect(function()
    local character = script.Parent.Parent
    local player = game:GetService("Players"):GetPlayerFromCharacter(character)
    local leaderstats = player:WaitForChild("leaderstats")
    local Strength = leaderstats:WaitForChild("Strength")

    Strength.Value = Strength.Value + 1
end)

And then it's basically the same script that you have for the server script in ServerScriptService, just remove the strength part, as that's should be in the sword or whatever you'll be doing with this.

ServerScript inside ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
    local sell = game:WaitForChild("ReplicatedStorage"):FindFirstChild("Sell")

    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = player
    leaderstats.Name = "leaderstats"

    local Strength = Instance.new("IntValue")
    Strength.Name = "Strength"
    Strength.Parent = leaderstats

    local Cash = Instance.new("IntValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    sell.OnServerEvent:Connect(function(player)
        Cash.Value = Strength.Value + Cash.Value
        Strength.Value = 0
    end)

    player.CharacterAppearanceLoaded:Connect(function(character)
        local humaniod = character.Humanoid
        humaniod:WaitForChild("BodyDepthScale").Value = .5 + (Strength.Value / 100)
        humaniod:WaitForChild("BodyHeightScale").Value = .5 + (Strength.Value / 100)
        humaniod:WaitForChild("BodyWidthScale").Value = .5 + (Strength.Value / 100)
        humaniod:WaitForChild("HeadScale").Value = .5 + (Strength.Value / 100)
        humaniod.WalkSpeed = 16 + (Strength.Value / 10)
        humaniod.MaxHealth = 100 + (Strength.Value / 10)

        Strength:GetPropertyChangedSignal("Value"):Connect(function(value)
            humaniod:WaitForChild("BodyDepthScale").Value = .5 + (value/ 100)
            humaniod:WaitForChild("BodyHeightScale").Value = .5 + (value/ 100)
            humaniod:WaitForChild("BodyWidthScale").Value = .5 + (value/ 100)
            humaniod:WaitForChild("HeadScale").Value = .5 + (value/ 100)
            humaniod.WalkSpeed = 16 + (value/ 10)
            humaniod.MaxHealth = 100 + (value/ 10)
        end)
    end)
end)

I made a little bit of tweaks, and just for your information. You cannot use game.Players.LocalPlayer inside of ServerScripts.

If you need more information on RemoteEvents, I answered a post about RemoteEvents here. Hope this helps!

If this worked for you, let me know by selecting this as the answer. If it didn't comment down below what went wrong and I'll be glad to help!

0
GeneratedScript's version also works as well! killerbrenden 1537 — 4y
0
How would I also apply this to a button because I'm gonna add a sell button Brockanthony10 19 — 4y
0
To do this for a sell button, in a LocalScript inside of the button you would do the click function and then fire the server, and then from the server remove their strength and convert the amount that you want into however much you want. killerbrenden 1537 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Because you're handling the OnServerEvent on the same script that handles CharacterAdded. Now this would not be an issue, but you make it set Cash.Value. Cash was declared above as the character that was added. This will happen everytime someone joins. You can fix this by changing your script:

local player = game.Players.LocalPlayer
local lift = game.ReplicatedStorage.Lift
local sell = game.ReplicatedStorage.Sell

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

    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = player
    leaderstats.Name = "leaderstats"

    local Strength = Instance.new("NumberValue")
    Strength.Name = "Strength"
    Strength.Parent = leaderstats

    local Cash = Instance.new("NumberValue")
    Cash.Name = "Cash"
    Cash.Parent = leaderstats

    lift.OnServerEvent:Connect(function(plr)
    local leaderstats, strength, cash = plr:WaitForChild("leaderstats"), leaderstats:WaitForChild("Strength"), leaderstats:WaitForChild("Cash"); -- Get the player who clicked and their stats
        strength.Value = strength.Value + 1
    end)

    sell.OnServerEvent:Connect(function(plr)
    local leaderstats, strength, cash = plr:WaitForChild("leaderstats"), leaderstats:WaitForChild("Strength"), leaderstats:WaitForChild("Cash"); -- Same thing here
        cash.Value = strength.Value + cash.Value
        strength.Value = 0
    end)

    player.CharacterAppearanceLoaded:Connect(function(character)
        local humaniod = character.Humanoid
    local plr = game.Players:GetPlayerFromCharacter(character);
    local leaderstats, strength, cash = plr:WaitForChild("leaderstats"), leaderstats:WaitForChild("Strength"), leaderstats:WaitForChild("Cash");
        humaniod:WaitForChild("BodyDepthScale").Value = .5 + (strength.Value / 100)
        humaniod:WaitForChild("BodyHeightScale").Value = .5 + (strength.Value / 100)
        humaniod:WaitForChild("BodyWidthScale").Value = .5 + (strength.Value / 100)
        humaniod:WaitForChild("HeadScale").Value = .5 + (strength.Value / 100)
        humaniod.WalkSpeed = 16 + (strength.Value / 10)
        humaniod.MaxHealth = 100 + (strength.Value / 10)

        strength:GetPropertyChangedSignal("Value"):Connect(function()
            humaniod:WaitForChild("BodyDepthScale").Value = .5 + (strength.Value / 100)
            humaniod:WaitForChild("BodyHeightScale").Value = .5 + (strength.Value / 100)
            humaniod:WaitForChild("BodyWidthScale").Value = .5 + (strength.Value / 100)
            humaniod:WaitForChild("HeadScale").Value = .5 + (strength.Value / 100)
            humaniod.WalkSpeed = 16 + (strength.Value / 10)
            humaniod.MaxHealth = 100 + (strength.Value / 10)
        end)
    end)
end)

Answer this question