Hi,
So I’m having trouble firing back to the Client after I’ve already fired to the Server and declared an OnServerEvent. The purpose of this is because I want to move the character to a new position via the body position. The body position (as well as body gyro) is created and positioned in the Local script because I need to constantly check for the mouse’s position in order to rotate the character towards it, and for aiming purposes.
(You basically hold down E and you’re suspended in the air, and when you release, you will be in hit’s position.)
I want the new body position after it fires back to the client to be (hit) which is where the ray got intersected (it’s already defined, which is why I’m sending the position to the client).
I apologize for my messy code in advance I am extremely new to this lol.
Local Scr
local inputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Char = Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local Remote = script:WaitForChild("RemoteEvent")
local Pressed = false
local Enabled = true
local Key = nil
local function coolDown()
Enabled = true
end
local function bodyStationary()
local BP = Instance.new("BodyPosition",Char.HumanoidRootPart)
BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BP.Position = Char.HumanoidRootPart.Position
local BG = Instance.new("BodyGyro", Char.HumanoidRootPart)
BG.P = 25000
BG.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
while Pressed == true do
BG.CFrame = CFrame.new(Char.HumanoidRootPart.Position, Mouse.Hit.Position)
runService.Heartbeat:Wait()
end
wait(0.5)
BG:Destroy()
end
inputService.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.E and Enabled == true and Char:FindFirstChild("Pika") then
Enabled = false
Pressed = true
Remote:FireServer("LightSword")
Key = "E"
bodyStationary()
elseif input.KeyCode == Enum.KeyCode.E and not Char:FindFirstChild("Pika") then end
end)
inputService.InputEnded:Connect(function(input, isTyping)
if isTyping then return end
if input.KeyCode == Enum.KeyCode.E and Key == "E" then
Pressed = false
Key = nil
script.RemoteEvent:FireServer("ERelease", Char.HumanoidRootPart.Position, Mouse.Hit.Position)
delay(5,coolDown)
end
end)
--Where problem is (1/2)
Remote.OnClientEvent:Connect(function(player, hit, text)
if text == "BodyNewPos" then
print("Does this work?")
Char.HumanoidRootPart.BodyPosition.Position = hit
end
end)
Server
local Player = game:GetService("Players")
local StarterPack = game:GetService("StarterPack")
local Remote = script.Parent
local debounce = false
local buttonDown = true
local MousePos1 = nil
local TorsoPos = nil
local char = nil
Remote.OnServerEvent:Connect(function(player,action,RightHand,MousePos)
if action == "ERelease" then
buttonDown = false
TorsoPos = RightHand
MousePos1 = MousePos
end
if action == "LightSword" and debounce == false then
buttonDown = true
char = player.Character
local function rayCasting()
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = {workspace.Game, char}
rayParams.IgnoreWater = false
local hit = workspace:Raycast(TorsoPos, CFrame.new(TorsoPos,MousePos1).LookVector*25000, rayParams)
if hit then
return hit.Position
else return nil
end
end
debounce = true
local An = Instance.new("Animation")
An.AnimationId = "rbxassetid://11992916940"
local SwordAnim = char.Humanoid:LoadAnimation(An)
SwordAnim:Play()
wait(1.35)
SwordAnim:AdjustSpeed(0)
while buttonDown == true do
wait()
end
local hit = rayCasting(TorsoPos,MousePos1,char)
if not hit or hit and (TorsoPos-hit).magnitude > 300 then
hit = CFrame.new(TorsoPos,MousePos1) * CFrame.new(0,0,-300).Position
char.HumanoidRootPart.CFrame = CFrame.new(hit)
elseif hit or not hit and (TorsoPos-hit).magnitude < 300 then
char.HumanoidRootPart.CFrame = CFrame.new(hit)
end
--Where problem occurs (2/2)
Remote:FireClient(player, hit, "BodyNewPos")
local Distance = (TorsoPos-hit).magnitude
SwordAnim:AdjustSpeed(1)
debounce = false
end
end)