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

How do I make this script run even faster, without the code "wait()" and without lagging?

Asked by 4 years ago

I would like to make this script about giving strength even faster to achieve so people wouldn't have to wait so much. I can only put a wait() script, but how can I remove it and not make this script wait in 0 seconds? (local script)

local savedStrengthValue = 0
local strengthValue = 0
script.Parent.MouseButton1Click:Connect(function()
    savedStrengthValue = tonumber(script.Parent.StrengthVal.Text)
    strengthValue = strengthValue + savedStrengthValue
    repeat
        game.ReplicatedStorage.remotes.lifts.CustomLift:FireServer(game.Players.LocalPlayer)
        strengthValue = strengthValue - 1
        wait()
    until strengthValue == 0
end)

Please fix this.

0
I know that I can use RunService:RenderStepped but there is a MouseButton1Click function above it and I can't break out of the loop. romoney5 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
-- Server

game.ReplicatedStorage.remote.lifts.CustomLift.OnServerEvent:Connect(function(player, val)
    -- make the server side increment the strength value by val isntead of just 1
end)


-- Client
local savedStrengthValue = 0
local strengthValue = 0
script.Parent.MouseButton1Click:Connect(function()
    savedStrengthValue = tonumber(script.Parent.StrengthVal.Text)
    strengthValue = strengthValue + savedStrengthValue
    game.ReplicatedStorage.remotes.lifts.CustomLift:FireServer(strengthValue)
end)

As a side note, this is very insecure way to approach this problem. Players could send requests to their server by spoofing remove event invokes. Consider a different method. :)

0
You made a typo (i fixed it), besides, doing this would throw another error: It can't add from number and userdata. (attempt to perform arithmetic (add) on number and userdata) romoney5 0 — 4y
0
The userdata is a player. The first parameter in the parameter list of the OnServer event function is a player. The following parameters are what you passed from the client. In other words, the first parameter of any remote event or function on the server is always the player that send that signal. You need to change the parameters in the server script and change the code too. EncapsuIation 450 — 4y
0
But what do I do to fix it? romoney5 0 — 4y
0
The question is answered, but you still did part of it correctly. romoney5 0 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I noticed that the first variable in a RemoteEvent does not work. Here is how I would fix this:

--Client
local savedStrengthValue = 0
script.Parent.MouseButton1Click:Connect(function()
    savedStrengthValue = tonumber(script.Parent.StrengthVal.Text)
    game.ReplicatedStorage.remotes.lifts.CustomLift:FireServer(game.Players.LocalPlayer,savedstrengthValue)
end)

--Server
game.ReplicatedStorage.remote.lifts.CustomLift.OnServerEvent:Connect(function(a,player, val)
    player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + val
end)

I was supposed to add an "a" behind the "player" variable, but I don't know why.

Answer this question