I am trying to make a value increase as you walk. I set up a code that checks if the player is pressing W, A, S, or D and will fire an event that increases the value, however I am struggling to find a way that will repeat for the entire duration that the keys are held down. Here is the script;
Script
local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Debounce = true
UIS.InputBegan:connect(function(key, Plr)
if Debounce == true then
if key.KeyCode == Enum.KeyCode.W or Enum.KeyCode.D or Enum.KeyCode.A or Enum.KeyCode.S then
Debounce = false
game.ReplicatedStorage.Speed:FireServer()
wait(0.1)
Debounce = true
end
end
end)
(I fire an event so that the value increases on server for data store)
I’m pretty sure the HumanoidRootPart stays still with animations.
Every other bodypart is relative to the HumanoidRootPart but the HumanoidRootPart is not animated.
Try this. I used the Heartbeat function of the RunService instead of the while loop so other script can run simultaneously in the same script. I’ve also used @dispeller 's method of using the Magnitude of the humanoid. Hope this helps!
local Player = game.Players.LocalPlayer
local HumanoidRootPart = Player.Character:WaitForChild("HumanoidRootPart")
local RS = game:GetService("RunService")
local debounce = false
local function testDebounce()
if debounce then
return
else
debounce = true
game.ReplicatedStorage.Speed:FireServer()
wait(0.3) --How ever long you want your delay between event calls
debounce = false
end
end
RS.Heartbeat:Connect(function()
if HumanoidRootPart.Velocity.Magnitude > 1 then
testDebounce()
end
end)
It stores the players’ speed. All it does is fire a remote event that triggers this; Speed.Value = Speed.Value + 1. If I don’t do this then Speed is stored on the client and I can’t save it with data store
I’m just concerned that maybe you’re asking how to solve problem for a solution rather than the underlying problem itself, what is the data store there for, are you just having a mess about with it is what I mean
I’m not really sure what you’re asking. The datastore is completely different and won’t affect the problem I am having with the remote event only firing once.
Don’t worry about it, for some reason you want a data store for the player’s speed, with no reason why. I was just thinking maybe there’s a better way of doing whatever you’re doing with the data store.
No, no, no. The datastore is seperate. It fires an event to add to the players’ IntValue. The script is a local script in StatterCharacterScripts and if I didn’t fire a remote event to increase the speed then the datastore wouldn’t work because the IntValue would have only been updated on the client.
Not sure if you edited it or I am just tired, but the game is called Flash Simulator where a value Speed increases as you walk.
Hello, just curious about two things. Did AstonAceMan’s solution work for you? Or did it yield the same results, I didn’t see a reply from you explicitly stating your results.
Also from your statement you mention:
All it does is fire a remote event that triggers this; Speed.Value = Speed.Value + 1
Is that all that’s happening when it’s triggered or are you also updating your DataStore in the same bit?
I know this isn’t exactly relevant to your problem at hand, but do you verify this at all on the server side, so say John Doe can’t come along and fire the remote as much as he likes?
You can just use the @AstonAceMan’s method but handled by the server so you don’t have to worry about an exploiter firing a Remote Event to get points:
local Players = game:GetService("Players")
local RS = game:GetService("RunService")
local debounce = false
Players.PlayerAdded:Connect(function(player)
local SpeedValue = --Rute of the Speed value
RS.Heartbeat:Connect(function()
local Character = player.Character or player.CharacterAdded:Wait()
local HumanoidRootPart = Character.HumanoidRootPart
if HumanoidRootPart.Velocity.Magnitude > 1 then
if debounce then
return
else
debounce = true
SpeedValue.Value = SpeedValue.Value + 1
wait(0.3)
debounce = false
end
end
end)
end)
I tested my code but with a print function and it worked. Did you get the same results with the event?
If it’s what you want then great, but I would definitely recommend @geovanny4567 ‘s method on the server.