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 create a remote function for this local script?

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

✓ Accepted answer

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!

2 comments

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.

0

Mhm.

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