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

How do i create a remote function for this local script?

Asked by 4 years ago

So i'm trying to make it so when you left click you punch and when you punch your strength stat goes up in leaderstats..It goes up but only on the client and i can't figure out how to script the OnServerEvent and the FireServer function in order to make this a server side script. I just started learning to script so try to explain in easily.

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local AnimationId = 5005123017

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://"..AnimationId

local AnimationTrack = Humanoid:LoadAnimation(Animation)

local function PunchAndAddStrength()
    if not (AnimationTrack.Stopped) then return end
    local Leaderstats = Player:WaitForChild("leaderstats")
    local Strength = Leaderstats:FindFirstChild("Strength")
    if (Strength) then
        Strength.Value = (Strength.Value + 1)
    end
    AnimationTrack:Play()
end

Mouse.Button1Down:Connect(PunchAndAddStrength)

1 answer

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

Yes, that is the really cause. You'll need to use RemoteEvents and an event listener.

First off, you'll need to insert a RemoteEvent in ReplicatedStorage and don't name it. That being done, you'll need to fire the RemoteEvent in the ReplicatedStorage through your local script.

The local script:

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local AnimationId = 5005123017

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://"..AnimationId

local AnimationTrack = Humanoid:LoadAnimation(Animation)

local function PunchAndAddStrength()
    if not (AnimationTrack.Stopped) then return end
    AnimationTrack:Play()
    game.ReplicatedStorage.RemoteEvent:FireServer() -- Fire's the remote event
end

Mouse.Button1Down:Connect(PunchAndAddStrength)

Now, insert a script into ServerScriptService and it would be as follows:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)
        player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
end)

Please make sure to upvote this question and select this as your answer if it helped!

0
Actually you can create the RemoteEvent from within the server script, and you can in fact name it just as anything else. It is actually encouraged to rename RemoteEvents if you have more than one. SteamG00B 1633 — 4y
0
Mhm. SilentsReplacement 468 — 4y
Ad

Answer this question