Hi, I am a new scripter, I created my first game not long ago and it was only for PC. I wanted to try to insert commands for mobile players (mechanics in my game: crouch, sprint) But to sprint the script doesn't work, I don't understand why but it certainly has to do with the fact that the image button is in one Gui and the script for sprinting in another. I could very well use an event but the problem is that my sprint is linked to a bar that downloads with a TweetSize showing the player how much you have left at any given moment. Could anyone help me? (Roblox shows me no errors)
SPRINT BAR SCRIPT (NAME: "MobileSprintScript")
--// VARIABLE \--
local UIS = game:GetService('UserInputService') local m = game.Players.LocalPlayer:GetMouse() local mm = game.StarterGui.CrouchGui.ActivateSprint local Bar = script.Parent:WaitForChild('SprintMobile'):WaitForChild(('Bar')) local player = game.Players.LocalPlayer local NormalWalkSpeed = 10 local SprintWalkSpeed = 20 local ii = 10 local running = false repeat wait() until game.Players.LocalPlayer.Character local character = player.Character
--// FUNCTIONS \--
mm.KeyDown:connect(function(key, gameProcessed) if key and gameProcessed == false then character.Humanoid.WalkSpeed = SprintWalkSpeed running = true while ii > 0 and running do ii = ii - 0.27 Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
wait() if ii <= 0 then character.Humanoid.WalkSpeed = NormalWalkSpeed end end end
end)
mm.KeyDown:connect(function(key, gameProcessed) if key and gameProcessed == false then character.Humanoid.WalkSpeed = NormalWalkSpeed running = false while ii < 10 and not running do ii = ii + .08 Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', .1, true)
wait() if ii <= 0 then character.Humanoid.WalkSpeed = NormalWalkSpeed end end end
end)
some mistakes: the first few lines in the variables
you could've done this:
local player = game:GetService("Players").LocalPlayer -- use getservice local m = player:GetMouse()
and save a bit of time from typing something you don't have to type twice
you should use :Connect over :connect, i think :connect is deprecated already.
task.wait() should be used over wait(), evaera has good reasons why in the following gist:
speaking of wait(), don't use unnecessary polling (repeat task.wait()s), repeat task.wait() until player.Character can be replaced with player.CharacterAdded:Wait(). even better, you can do this:
local character = player.Character or player.CharacterAdded:Wait()
if player's Character already exists, character = player.Character otherwise, character will be whatever player.CharacterAdded:Wait() returns (the player's character, which just spawned)
userinputservice and mouse is not used, consider removing the variable uis and m
you can use button.MouseButton1Down and button.MouseButton1Up instead of KeyDown and KeyUp
main problem you are referencing the startergui, but you should reference the localplayer's player gui, player.PlayerGui
anyways, here is the fixed script (i also improve it)
-- btw when posting code in this website, you should put code in a code block by pressing a lua symbol -- put this local script in startergui.crouchgui local PS = game:GetService("Players") local RnS = game:GetService("RunService") -- h local TS = game:GetService("TweenService") -- you don't have to do this, but i'll use tweenservice for this. local plr = PS.LocalPlayer local chr = plr.Character or plr.CharacterAdded:Wait() local hum = chr:WaitForChild("Humanoid") local ui = script.Parent -- when the ui is cloned to playergui, it will still reference the ui in playergui, which is cool local mm = ui:WaitForChild("ActivateSprint") local bar = ui:WaitForChild("SprintMobile"):WaitForChild("Bar") -- this is assuming the script you use is already parented to the startergui local running = false local ii = 10 local normalws = 10 local runningws = 20 RnS:BindToRenderStep("Running", 0, function() -- i replace while true loop with RunService:BindToRenderStep(name, priority, function) because i like fancy e.e bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, 0.1, true) if running then ii -= 0.27 if ii <= 0 then TS:Create(hum, TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {WalkSpeed = normalws}):Play() running = false end else if ii < 10 then ii += 0.08 end end end) mm.MouseButton1Down:Connect(function() TS:Create(hum, TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.In), {WalkSpeed = runningws}):Play() -- make the transition to running smoother running = true end) mm.MouseButton1Up:Connect(function() TS:Create(hum, TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.In), {WalkSpeed = normalws}):Play() running = false end)
Links that help: