Hello Guys!
Hope your day is going well, but sadly mine has not because of raycasting.
I want to make a raycast ability which gets fired from a keybind (I used F). Here is the script from the Client:
--//Services and Player
local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local DS = game:GetService("Debris")
local Player = PS.LocalPlayer
local Camera = workspace.CurrentCamera
--//Remotes
local RayExecutionServiceRS = RS:WaitForChild("ServicesReplications").RayExecutionService
local ExecuteRayRemote = RayExecutionServiceRS.ExecuteRay
--//Gui
local RayExecuterGui = script.Parent
local ExecuteRayButton = RayExecuterGui.ExecuteButton
local EnabledLabel = RayExecuterGui.EnabledLabel
local CooldownFrame = RayExecuterGui.CooldownFrame
--//Modules
local CoreContextModule = require(RayExecuterGui.Parent.Parent.Parent.ClientGui.ContextUtilities.CoreContext)
--//Variables and Debounce
local DB = false
local ModeSelect = false
--//Functions
local function ToggleModeSelect()
if ModeSelect == false and DB == false then
ModeSelect = true
ExecuteRayButton.UIStroke.Color = Color3.fromRGB(0, 121, 30)
EnabledLabel.Text = "Enabled"
EnabledLabel.TextColor3 = Color3.fromRGB(0, 255, 8)
elseif ModeSelect == true then
ModeSelect = false
ExecuteRayButton.UIStroke.Color = Color3.fromRGB(0,0,0)
EnabledLabel.Text = "Disabled"
EnabledLabel.TextColor3 = Color3.fromRGB(255, 0, 0)
end
end
local function CooldownThread(Cooldown)
task.spawn(function()
task.wait(Cooldown)
DB = false
end)
local Frame = CooldownFrame:Clone()
Frame.Visible = true
Frame.Parent = ExecuteRayButton
local Tween = TS:Create(Frame, TweenInfo.new(Cooldown, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Size = UDim2.fromScale(0,1)})
Tween:Play()
DS:addItem(Frame, Cooldown)
end
--MAIN EXECUTION
local function ExecuteClient()
local MousePosition = UIS:GetMouseLocation()
local MouseRay = Camera:ViewportPointToRay(MousePosition.X, MousePosition.Y)
local Execution, Cooldown, Return = ExecuteRayRemote:InvokeServer(MouseRay--[[MouseRay]])
if Execution then
warn("RayCast Executed Successfully. "..Return.." [RayExecutionService]") --toggle prints here.
task.spawn(CooldownThread, Cooldown)
elseif Execution == false then
warn("RayCast Failed, "..Return.." [RayExecutionService]")
task.wait(0.1)
DB = false
end
end
--//Connections
--Toggle Button/Input Connection
UIS.InputBegan:Connect(function(Input, GP)
if GP == true then return end
if Input.KeyCode == Enum.KeyCode.F then
ToggleModeSelect()
end
end)
ExecuteRayButton.MouseButton1Click:Connect(function()
ToggleModeSelect()
end)
--Firing Ray Connection
UIS.InputBegan:Connect(function(Input, GP)
if GP == true then return end
if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
if ModeSelect == true then
if DB == false then
DB = true
ExecuteClient()
ToggleModeSelect()
end
end
end
end)
Here is the serverscript which recieves the request:
--//Services and Player
local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")
--//Remotes
local RayExecutionServiceRS = RS:WaitForChild("ServicesReplications").RayExecutionService
local ExecuteRayRemote = RayExecutionServiceRS.ExecuteRay
--//Modules
local ConfigurationModule = require(script.Parent.Configurations)
local Settings = ConfigurationModule.Settings
--//Variables and Debounce
local Debounces = {}
--//Functions and Connections
local function CooldownThread(Player, Cooldown)
task.wait(Cooldown)
Debounces[Player] = nil
end
local function Execute(Player, MouseRay)
Debounces[Player] = true
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local ExecutingRay = Player:WaitForChild("ExecutingRay")
local Eliminated = Player:WaitForChild("Eliminated")
if ExecutingRay.Value then
return false, nil, "Player is still executing a ray!"
end
if Eliminated.Value then
return false, nil, "Player is eliminated!"
end
--Identify Raycast
local RCParams = RaycastParams.new()
RCParams.FilterType = Enum.RaycastFilterType.Exclude
RCParams.FilterDescendantsInstances = {Character}
local RaycastResult = workspace:Raycast(MouseRay.Origin, MouseRay.Direction * 15000, RCParams)
if RaycastResult == nil then
task.spawn(CooldownThread, Player, Settings.Cooldown)
return true, Settings.Cooldown, "Nothing Found - nil."
end
local TargettedCharacter = RaycastResult.Instance:FindFirstAncestorWhichIsA("Model")
local TargettedHumanoid = TargettedCharacter:FindFirstChildWhichIsA("Humanoid")
local TargettedPlayer = PS:GetPlayerFromCharacter(TargettedCharacter)
print(tostring(RaycastResult.Instance),TargettedCharacter,TargettedHumanoid,TargettedPlayer)
if TargettedHumanoid == nil and TargettedPlayer == nil then
task.spawn(CooldownThread, Player, Settings.Cooldown)
return true, Settings.Cooldown, "Nothing Found related to Character/Player!"
end
ExecutingRay.Value = true
--[[for i, Descendant in pairs(Character:GetDescendants()) do
if Descendant:IsA("BasePart") then
Descendant.Transparency = 1
Descendant.CanCollide = false
end
end]]
task.spawn(CooldownThread, Player, Settings.Cooldown)
ExecutingRay.Value = false
return true, Settings.Cooldown, "Player Found."
end
ExecuteRayRemote.OnServerInvoke = function(Player,Target)
--ERROR HANDLING: [Success, Cooldown, Error]
if Player == nil then
return false, nil, "Incorrect Paramaters!"
end
if Target == nil then
return false, nil, "Nothing Found!"
end
if Debounces[Player] then
return false, nil, "On Cooldown!"
end
local Execution, Cooldown, Return = Execute(Player, Target)
return Execution, Cooldown, Return
end
Now ofc the script works, but not in the intended way. When it directly fires WHEN MY MOUSE IS HITTING A BODY PART, it registers it as something else.
I want some ideas and scripting help on making this system better. I also want it to ignore things like accessories and just go through it directly.
Thanks for stopping by!