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)
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
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