Hi,
Post may be slightly vague, however can see any blatant issues with the script below? The script is a stamina function set to work only when a player has a certain team value as a StringValue
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local Bindable = Instance.new("BindableEvent")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local IsAllowed
local maxStamina = 250
local staminaRegen = 1
local staminaCost = 5
local sprintSpeed = 24
local walkSpeed = 16
local currentStamina = maxStamina
-- Update Stamina GUI
function updateGui(current, max)
if character.Humanoid.Health <= 0 or not IsAllowed then
player.PlayerGui.Stamina.Enabled = false
end
player.PlayerGui.Stamina.Bar.Size = UDim2.new((current / max)* 0.2, 0,0.05, 0)
end
-- Sprint Key Pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 and IsAllowed then
character.Humanoid.WalkSpeed = sprintSpeed
end
end
end)
-- Sprint Key Released
userInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift and IsAllowed then
character.Humanoid.WalkSpeed = walkSpeed
end
end)
player.SelectedTeam:GetPropertyChangedSignal("Value"):Connect(function()
Bindable:Fire(player.SelectedTeam.Value)
end)
Bindable.Event:Connect(function(Value)
local Con
Con = runService.Heartbeat:Connect(function()
if Value ~= "Lobby" then
IsAllowed = true
if character.Humanoid.WalkSpeed == sprintSpeed then
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost
else
character.Humanoid.WalkSpeed = walkSpeed
end
else
if currentStamina < maxStamina then
currentStamina = currentStamina + staminaRegen
elseif currentStamina > maxStamina then
currentStamina = maxStamina
end
end
updateGui(currentStamina, maxStamina)
else
IsAllowed = false
character.Humanoid.WalkSpeed = walkSpeed
updateGui(currentStamina, maxStamina)
if Con then
Con:Disconnect()
end
end
end)
end)
Edit: The script returns no errors on testing however does not work
What are you trying to ask? Is something not working right? If you are looking for someone to review your code, you should probably put it in Code Review
I noticed that the IsAllowed variable is not in set to true until the Bindable event is fired. Is the SelectedTeam value changed right when the player joins? If itâs not, then IsAllowed is not being set to true until SelectedTeam is changed (sorry if this didnât make sense).
As soon as the player joins the SelectedTeam value is set to âLobbyâ. About 15 seconds into the game starting the SelectedTeam value changes to something else, any thoughts on that? - I wanted it so that around 15 seconds into the game when the players load into the game map (and hence the SelectedTeam value changes) stamina is enabled
Hmm, I donât see any reason for it to not work. Can you send a video (even if it doesnât work)?
Also, do you think you could add some print functions so we can see what code is actually running?
Here:
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
print("Left Shift Pressed")
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 and IsAllowed then
character.Humanoid.WalkSpeed = sprintSpeed
print("Sprinted")
end
end
end)
Okay, so I added the two print statements that you suggested, and this is what happens: https://streamable.com/6p5jj2
And something really strange happens
So the first print statement works when having the âLobbyâ SelectedTeam value (not sure if this is meant to happen) however the second sprinting print statment doesnt work in the lobby.
On loading into the map (so the SelectedTeam values are changed to assassin, king etc), pressing left shift no longer prints either of the print statements at all, HOWEVER I have noticed when i hold left shift down while getting teleported, the sprint bar actually works in the map even though the player doesnt actually sprint any faster. (If i was to press nothing before loading in the sprint bar wouldnt even move like it does in the video)
(To avoid confusion i have teams setup in studio, and also SelectedTeam stringvalues to refer to in code)
hope that all makes sense?
One question, is this script in starterplayer or startercharacter? Because whenever a player get reset, the character is deleted and a new one is created. This means that the variables for the playerâs character are no longer valid. Of course, this is assuming that the player is reset when they load into the map, since it seems that way in the video.
So i only want the sprint to work when they get into the map (So it is disabled in the lobby)
And i have it in StarterCharacterScripts, not sure if this is an issue. Yes i suspected it has something to do with character resetting bugging it in some way
Yes, this is fine. The first print statement will run whenever left shift is pressed, regardless if the player is in the lobby or not.
This is also fine, since the script is deleted when the character is reset.
Uh oh, somethings up with this. The first print statement should always print when you press left shift. Can you try putting a print statement in front of the InputBegan event? This way we can see if the script is even making it here. (Also, please keep the other print statements)
print("Whatever You want")
userInputService.InputBegan -- Blah Blah Blah
Also, I donât see any reason to have a bindable event fire when the SelectedTeam value is fired. Why not just place the code in the GetPropertyChangedSignal event?
Ok, so ive added that print statement in, and on launching the test server it prints straight away, however when loading into the map it doesnt print againâŚ
And not quite sure what you mean with that last part, can you write out what you mean?
Let me know if you need to know anything more
Ok. Can you tell me what exactly happens when the map is loaded? Is the player reset? Or is their character just teleported?
What I mean is, why not just run the code for the bindable event in here?
Hereâs what the change would look like:
player.SelectedTeam:GetPropertyChangedSignal("Value"):Connect(function()
local Con
Con = runService.Heartbeat:Connect(function()
if Value ~= "Lobby" then
IsAllowed = true
if character.Humanoid.WalkSpeed == sprintSpeed then
if currentStamina >= staminaCost and character.Humanoid.MoveDirection.Magnitude > 0 then
currentStamina = currentStamina - staminaCost
else
character.Humanoid.WalkSpeed = walkSpeed
end
else
if currentStamina < maxStamina then
currentStamina = currentStamina + staminaRegen
elseif currentStamina > maxStamina then
currentStamina = maxStamina
end
end
updateGui(currentStamina, maxStamina)
else
IsAllowed = false
character.Humanoid.WalkSpeed = walkSpeed
updateGui(currentStamina, maxStamina)
if Con then
Con:Disconnect()
end
end
end)
end)
Ps: ive noticed on placing the script in the screengui i get the âLeft Shift pressedâ prints in the console when actually in the map during the round which seems to be better⌠(however the player isnt sprinting still)
To answer your questions:
They are âdressedâ in which their character is replaced by a premade model (skin) and then teleported into the workspace by CFrame.
This is the dress function for context
function module.DressBodyguard(bodyguard)
for i, v in pairs (bodyguard) do
if v.Character then
local hat
local hair
local character = v.Character
if v.EquippedBodyguardSkin.Value ~= "" then
if game.ReplicatedStorage.BodyguardSkins:FindFirstChild(v.EquippedBodyguardSkin.Value) then
character = game.ReplicatedStorage.BodyguardSkins[v.EquippedBodyguardSkin.Value]:Clone()
end
else
character = game.ReplicatedStorage.BodyguardSkins.Default:Clone()
end
if v.EquippedBodyguardHat.Value ~= "" then
if game.ReplicatedStorage.BodyguardHats:FindFirstChild(v.EquippedBodyguardHat.Value) then
hat = game.ReplicatedStorage.BodyguardHats[v.EquippedBodyguardHat.Value]:Clone()
end
else
hat = game.ReplicatedStorage.BodyguardHats.Default:Clone()
end
if v.EquippedHairstyle.Value ~= "" then
if game.ReplicatedStorage.Hairstyles:FindFirstChild(v.EquippedHairstyle.Value) then
hair = game.ReplicatedStorage.Hairstyles[v.EquippedHairstyle.Value]:Clone()
end
else
hair = game.ReplicatedStorage.Hairstyles.Default:Clone()
end
hair.Parent = character
hat.Parent = character
character.Name = v.Name
v.Character = character
character.Parent = workspace
end
end
end
(So not sure if player resets or not - but i think they do)
il try that and let you know if that does anything
Strangely trying that and putting the script in the screen gui ^,
One of the three players gets both the âLeft shift pressedâ and âSprintedâ prints with the stamina bar moving, however no movement increase. However, the other two didnt so i really dont know what is happening
This is probably because the output you are seeing is only for the client you are currently on when you are testing. If you switch windows to another client, you will see the same output for that client. This script runs on each client individually, so you wonât see those statements printed three times, only one.
Why thatâs not working, I have no idea. Do you have any other scripts adjusting player walkspeed?