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 5 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.

01local Player = game:GetService("Players").LocalPlayer
02local Mouse = Player:GetMouse()
03 
04local Character = Player.Character or Player.CharacterAdded:Wait()
05local Humanoid = Character:WaitForChild("Humanoid")
06 
07local AnimationId = 5005123017
08 
09local Animation = Instance.new("Animation")
10Animation.AnimationId = "rbxassetid://"..AnimationId
11 
12local AnimationTrack = Humanoid:LoadAnimation(Animation)
13 
14local function PunchAndAddStrength()
15    if not (AnimationTrack.Stopped) then return end
View all 24 lines...

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 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:

01local Player = game:GetService("Players").LocalPlayer
02local Mouse = Player:GetMouse()
03 
04local Character = Player.Character or Player.CharacterAdded:Wait()
05local Humanoid = Character:WaitForChild("Humanoid")
06 
07local AnimationId = 5005123017
08 
09local Animation = Instance.new("Animation")
10Animation.AnimationId = "rbxassetid://"..AnimationId
11 
12local AnimationTrack = Humanoid:LoadAnimation(Animation)
13 
14local function PunchAndAddStrength()
15    if not (AnimationTrack.Stopped) then return end
16    AnimationTrack:Play()
17    game.ReplicatedStorage.RemoteEvent:FireServer() -- Fire's the remote event
18end
19 
20Mouse.Button1Down:Connect(PunchAndAddStrength)

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

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

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 — 5y
0
Mhm. SilentsReplacement 468 — 5y
Ad

Answer this question