So I’m currently trying to set player’s humanoidRootPart velocity by pressing “e” on the keyboard.
I’m doing it through an event from the client to the server (to use the UserInputService).
The issue is that for some reason it doesn’t work on the server.
I tried searching on the devforum and outside of it but I didn’t find anything regarding this.
Client code:
local UIS = game:GetService("UserInputService")
local player = script.Parent
local event = game.ReplicatedStorage.Events.OnAdded.added
UIS.InputBegan:Connect(function(input, typing)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
if not typing then
event:FireServer(player)
end
end
end
end)
UIS.InputEnded:Connect(function(input, typing)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
if not typing then
event:FireServer(player)
end
end
end
end)
Server code:
local event = game.ReplicatedStorage.Events.OnAdded.added
local POWER
local POWER_MULTI = 150
local startTime
local endTime
local eventCounter = 0
event.OnServerEvent:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char.Humanoid
local HRP = char.HumanoidRootPart
if eventCounter == 0 then -- KEYDOWN --
startTime = DateTime.now().UnixTimestampMillis/1000
eventCounter += 1
elseif eventCounter > 0 then -- KEYUP --
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
return
end
endTime = DateTime.now().UnixTimestampMillis/1000
POWER = (endTime - startTime) * POWER_MULTI
print(POWER)
HRP.AssemblyLinearVelocity = Vector3.new(0, POWER, 0)
eventCounter = 0
end
end)
The script looks fine to me, have you tried print debugging to see which statements are running or not? It could also be because the POWER isn’t strong enough. When you printed power you did find that it was an actual number right?
Yes, I just checked if the POWER is a number value, and it is. I don’t think that it doesn’t work because it is not strong enough too (the number is always bigger than 0)
Ok I found the solution:
I’ve made so that it spawns an instance called “BodyVelocity” to make the player fling,
then I set the maxForce value to be math.huge on every axis so that it can go as fast as it can.
And for the final part I just set the task.wait() time to destroy the bodyVelocity (because if not destroyed it will go on forever).
In my case I just calculate the time according to the endTime - startTime number
result code:
local event = game.ReplicatedStorage.Events.OnAdded.added
local POWER
local POWER_MULTI = 150
local startTime
local endTime
local eventCounter = 0
local destroyTime
event.OnServerEvent:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char.Humanoid
local HRP = char.HumanoidRootPart
if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
return
end
if eventCounter <= 0 then -- KEYDOWN --
startTime = DateTime.now().UnixTimestampMillis/1000
eventCounter += 1
elseif eventCounter > 0 then -- KEYUP --
endTime = DateTime.now().UnixTimestampMillis/1000
POWER = (endTime - startTime) * POWER_MULTI
destroyTime = (endTime - startTime) / 5
local BodyVelocity = Instance.new("BodyVelocity", HRP)
BodyVelocity.Velocity = Vector3.new(HRP.Velocity.X,POWER,HRP.Velocity.Z)
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
task.wait(destroyTime)
BodyVelocity:Destroy()
eventCounter = 0
end
end)