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

How to pass variable(parameter) from localscript to server script using remoteEvent?

Asked by 5 years ago
Edited 5 years ago

Here is my localscript

player = game.Players.LocalPlayer

function addChar()
    wait(0.1)
    player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth+(2*Str)+(4*Vit)
    player.Character.Humanoid.Health = player.Character.Humanoid.MaxHealth
    script.Parent.Parent.Information.HP.Text = "HP: " .. tostring(player.Character.Humanoid.MaxHealth)
    script.Parent.Parent.Information.MP.Text = "MP: " .. tostring((player.Stats.Intelligence.Value*4)+100)
    script.Parent.Parent.Information.ATKSpeed.Text = "ATKSpeed: " .. tostring(Speed)
    script.Parent.Parent.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
    script.Parent.Parent.Information.Stamina.Text = "Stamina: " .. tostring(player.PlayerGui.mainUI.statHolder.staminaHolder.staminaBar.MaxStamina.Value)
    script.Parent.Parent.Information.CRITCHANCE.Text = "Critical %: " .. tostring(Critical) .. "%"
    script.Parent.Parent.Information.CRITDMG.Text = "Critical DMG: " .. tostring(CriticalDamage)
    script.Parent.Parent.Information.Evasion.Text = "Evasion%: " .. tostring(player.Stats.Dexterity.Value/5) .. "%"
    script.Parent.Parent.Information.SprintSpeed.Text = "Sprint Speed: " .. tostring(player.Stats.Agility.Value+26)
end

player.CharacterAdded:Connect(function()
    game.ReplicatedStorage.RemoteEvents.CharacterAdded:FireServer(player)
end)
player.Equip.ChildAdded:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Strength")
end)
player.Equip.ChildRemoved:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Strength")
end)
player.Stats.Strength.Changed:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Strength")
end)
player.Stats.Agility.Changed:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Agility")
end)
player.Stats.Intelligence.Changed:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Intelligence")
end)
player.Stats.Defense.Changed:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Defense")
end)
player.Stats.Vitality.Changed:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Vitality")
end)
player.Stats.Dexterity.Changed:Connect(function()
    game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(player, "Dexterity")
end)

my server script:

game.ReplicatedStorage.RemoteEvents.StatChange.OnServerEvent:connect(
function(player, x)
    wait(0.03)
    print(x)
    local Str = player.Stats.Strength.Value
    local Agi = player.Stats.Agility.Value
    local Int = player.Stats.Intelligence.Value
    local Def = player.Stats.Defense.Value
    local Vit = player.Stats.Vitality.Value
    local Dex = player.Stats.Dexterity.Value
    local Damage = 0
    local Speed = 0
    local Critical = 0
    local CriticalDamage = 0
    local weapon

    for j,k in pairs(player.Equip:GetChildren()) do
        if k.itemType.Value == "Weapon" then
            Damage = k.itemDamage.Value
            Speed = k.itemSpeed.Value
            Critical = k.itemCritical.Value
            CriticalDamage = k.itemCritDamage.Value
            weapon = k
            player.PlayerGui.statsGUI.Main.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
            player.PlayerGui.statsGUI.Main.Information.CRITCHANCE.Text = "Critical %: " .. tostring(Critical) .. "%"
            player.PlayerGui.statsGUI.Main.Information.CRITDMG.Text = "Critical DMG: " .. tostring(CriticalDamage)
        end
    end

    if weapon == nil then
        Damage = 0
        Speed = 0
        Critical = 0
        CriticalDamage = 0
        player.PlayerGui.statsGUI.Main.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
    end

        --Health--
    if x == "Strength" then
        player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth+2
        player.PlayerGui.statsGUI.Main.Information.HP.Text = "HP: " .. tostring(player.Character.Humanoid.MaxHealth)
        player.PlayerGui.statsGUI.Main.Information.ATKDMG.Text = "ATKDMG: " .. tostring(Damage)
    end
    print("Lmao")
    if x == "Agility" then
        player.PlayerGui.statsGUI.Main.Information.ATKSpeed.Text = "ATKSpeed: " .. tostring(Speed)
        player.PlayerGui.statsGUI.Main.Information.SprintSpeed.Text = "Sprint Speed: " .. tostring(player.Stats.Agility.Value+26)
    end
    if x == "Intelligence" then
        player.PlayerGui.statsGUI.Main.Information.MP.Text = "MP: " .. tostring((player.Stats.Intelligence.Value*4)+100)
    end
    if x == "Defense" then
        player.PlayerGui.statsGUI.Main.Information.damageResistance.Text = "DMG Resist%: " .. tostring(player.Stats.Defense.Value/5) .. "%"
    end
    if x == "Vitality" then
        player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth+4
        player.PlayerGui.statsGUI.Main.Information.HP.Text = "HP: " .. tostring(player.Character.Humanoid.MaxHealth)
        player.PlayerGui.statsGUI.Main.Information.Stamina.Text = "Stamina: " .. tostring(player.PlayerGui.mainUI.statHolder.staminaHolder.staminaBar.MaxStamina.Value)
    end
    if x == "Dexterity" then
        player.PlayerGui.statsGUI.Main.Information.CRITCHANCE.Text = "Critical %: " .. tostring(Critical) .. "%"
        player.PlayerGui.statsGUI.Main.Information.CRITDMG.Text = "Critical DMG: " .. tostring(CriticalDamage)
        player.PlayerGui.statsGUI.Main.Information.Evasion.Text = "Evasion%: " .. tostring(player.Stats.Dexterity.Value/5) .. "%"
    end
end)

it prints x as my player name?

0
You should use more variables. Your code is cut off. User#19524 175 — 5y
0
wait ill put my whole code xxXTimeXxx 101 — 5y
0
there xxXTimeXxx 101 — 5y
0
Any errors? User#19524 175 — 5y
View all comments (4 more)
0
None, it just prints x as my player name instead of the intended parameter xxXTimeXxx 101 — 5y
0
What does your FireServer look like? It’s cut off. User#19524 175 — 5y
0
i will seperate it using enter Edited. xxXTimeXxx 101 — 5y
0
You’re editing the PlayerGui from the server. Not the way to go. User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

If you want to pass a variable parameter to from your local script to your server script using a Remote Event, you need to do this:

Local Script:

game.ReplicatedStorage.RemoteEvents.StatChange:FireServer(VariableYouWantToPass)

Server Script:

game.ReplicatedStorage.RemoteEvents.StatChange.OnServerEvent:connect(
function(player, x)

-- Your code

end)

In this case whatever variable you want to pass from your local script will be viewed as "x" in your server script

0
I see, so you don't need to input player as a parameter? Thanks :D xxXTimeXxx 101 — 5y
0
Np skate992 57 — 5y
Ad

Answer this question