Client to Client remote event not working. I have a localscript under a button which is meant to fire a remote event when it is clicked. The remote event is then meant to be received by another localscript in starterplayerscripts which then activates sprint. The problem is, its not working and I cannot find out why.
Button script:
local sprintButton = script.Parent
local sprintEvent = game.ReplicatedStorage:WaitForChild("SprintEvent")
sprintButton.MouseButton1Down:Connect(function()
sprintEvent:FireServer()
end)
Starterplayerscripts script:
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character
local humanoid
local isSprinting = false
local sprintDuration = 5 -- Adjust this value to set the maximum sprint duration in seconds
local sprintSpeed = 40 -- Adjust this value to set the sprinting speed
local normalSpeed = 20 -- Adjust this value to set the normal walking speed
local sprintCooldown = 10 -- Adjust this value to set the cooldown duration in seconds
local sprintTimer = 0 -- Timer to track the remaining sprint duration
local cooldownTimer = 0 -- Timer to track the cooldown
local sprintButton = Enum.KeyCode.LeftShift
player.CharacterAdded:Connect(function(char)
character = char
humanoid = char:WaitForChild("Humanoid")
end)
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == sprintButton and not isSprinting and cooldownTimer <= 0 then
StartSprint()
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == sprintButton and isSprinting then
StopSprint()
end
end)
function StartSprint()
isSprinting = true
humanoid.WalkSpeed = sprintSpeed
sprintTimer = sprintDuration
cooldownTimer = sprintCooldown
end
function StopSprint()
isSprinting = false
humanoid.WalkSpeed = normalSpeed
end
RunService.RenderStepped:Connect(function()
if character and character.PrimaryPart and player.Character and player.Character:FindFirstChildOfClass("Humanoid") then
local root = character.PrimaryPart
local moveDirection = mouse.Hit.Position - root.Position
local moveMagnitude = moveDirection.Magnitude
if sprintTimer > 0 then
sprintTimer -= RunService.RenderStepped:Wait()
elseif isSprinting then
StopSprint()
end
if cooldownTimer > 0 then
cooldownTimer -= RunService.RenderStepped:Wait()
end
local speed = isSprinting and sprintSpeed or normalSpeed
if moveMagnitude > 0 then
humanoid:MoveTo(root.Position + moveDirection.Unit * speed)
end
end
end)
local sprintEvent = game.ReplicatedStorage:WaitForChild("SprintEvent")
sprintEvent.OnClientEvent:Connect(function()
if not isSprinting and cooldownTimer <= 0 then
StartSprint()
end
end)
this is the specific remote event part for the starterplayerscripts:
local sprintEvent = game.ReplicatedStorage:WaitForChild("SprintEvent")
sprintEvent.OnClientEvent:Connect(function()
if not isSprinting and cooldownTimer <= 0 then
StartSprint()
end
end)
RemoteEvents are for communicating between the client and the server, not from one LocalScript to another. If you want that behavior, you should instead use a BindableEvent.
BindableEvents allow you to register a callback across scripts in the same context (client-side LocalScript to another LocalScript, or server-side Script to another Script).
So convert the SprintEvent
to a BindableEvent, then try this in your button...
local sprintButton = script.Parent
local sprintEvent = game.ReplicatedStorage:WaitForChild("SprintEvent")
sprintButton.MouseButton1Down:Connect(function()
sprintEvent:Fire()
end)
And then in the StarterPlayerScripts...
local sprintEvent = game.ReplicatedStorage:WaitForChild("SprintEvent")
sprintEvent.Event:Connect(function()
if not isSprinting and cooldownTimer <= 0 then
StartSprint()
end
end)