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

I'm trying to make it so when you click a button it adds +1 to the leaderboard, see anything wrong?

Asked by 3 years ago
Edited 3 years ago

I put a local script inside of a textbutton and its suppose to make the train move and also add 1 to the players stats. But it only moves the train and doesn't add anything to the player's stats.

local Train = game.Workspace.Train




function move()
for v = 0,1,.05 do 
Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0, -v))
    wait()
end

    while wait() do

    Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0,0 + -1.9))
end

end


function Give()
game.Players.LocalPlayer.TimesRanOver.Value = game.Players.LocalPlayer.TimesRanOver.Value +1
end




script.Parent.MouseButton1Click:Connect(function(plr)

script.Parent:Destroy()

move()
Give()


end)
0
Is there an error message? Also the value is changed only from the client from this script. Dannys72344 44 — 3y
0
What do you mean the value can only be changed by the client of this script? stickymirro511 61 — 3y
0
The leaderstats dont replicate to the server ,only the client. The way to fix this is to either put this code inside a normal script or using remote events to fire a server script. JustinWe12 723 — 3y

2 answers

Log in to vote
1
Answered by
Leamir 3138 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

You can only destroy script.Parent after you execute the script, or you will just destroy the script and it will stop running

local Train = game.Workspace.Train




function move()
for v = 0,1,.05 do 
Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0, -v))
    wait()
end

    while wait() do

    Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0,0 + -1.9))
end

end


function Give()
game.Players.LocalPlayer.TimesRanOver.Value = game.Players.LocalPlayer.TimesRanOver.Value +1
end




script.Parent.MouseButton1Click:Connect(function(plr)

move()
Give()
script.Parent:Destroy()

end)

Also, this should be a server script, try using Remote Events

Ad
Log in to vote
-1
Answered by 3 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Here Is The Script I Haven't Tested It So idk If It Will Work But Here

local Train = game.Workspace.Train




function move()
for v = 0,1,.05 do 
Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0, -v))
    wait()
end

    while wait() do

    Train:SetPrimaryPartCFrame(Train:GetPrimaryPartCFrame() * CFrame.new(0,0,0 + -1.9))
end

end


function Give()
game.Players.LocalPlayer.TimesRanOver.leaderstats.Value = game.Players.LocalPlayer.TimesRanOver.leaderstats.Value +1
end




script.Parent.MouseButton1Click:Connect(function(plr)

script.Parent:Destroy()

move()
Give()


end)

If It Works Than Please Accept My Answer

0
Doesn't work, sorry. stickymirro511 61 — 3y

Answer this question