Hi I have been trying to raycast to see if a player is in front of you once you equip a tool when a key is pressed so that if a player is 20 studs or less away from you it fires a remote event I’ve coded the key press part but I’m struggling with the raycast and then firing the remote event in the tool
Heres my code:
local Plr = game.Players.LocalPlayer
character = Plr.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local Plrs = game.Players
local result = workspace:Raycast(Plr.Character.Head.Position, Plr.Character.Head.CFrame.LookVector*20)
local target_player = Plrs:GetPlayerFromCharacter(result.Instance.Parent)
local AnimationTrack = Instance.new("Animation")
AnimationTrack.AnimationId = "rbxassetid://507770453"
local AnimationTrack = Animator:LoadAnimation(AnimationTrack)
AnimationTrack.Priority = Enum.AnimationPriority.Action
AnimationTrack.Looped = false
local UserInPutService = game:GetService("UserInputService")
UserInPutService.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.R then
if Plr.Backpack:FindFirstChild("Knockback") then
local tool = Plr.Backpack:FindFirstChild("Knockback")
humanoid:EquipTool(tool)
AnimationTrack:Play()
script.Parent.KnockBack:FireServer(target_player)
humanoid.WalkSpeed = 0
AnimationTrack.Stopped:Wait()
humanoid:UnequipTools()
humanoid.WalkSpeed = 16
end
end
end)
The main problems are these lines:
local result = workspace:Raycast(Plr.Character.Head.Position, Plr.Character.Head.CFrame.LookVector*20)
local target_player = Plrs:GetPlayerFromCharacter(result.Instance.Parent)
FireServer() already gets the original player that fired the event
your server script, if it has a function with 2 arguments, function a(b,c)
b would then be the player that fired the event, and c would be the target player. if we could have some code of your server script that would be cool
Ok, My server script basically was from a tutorial which gives a knock back effect to the target player
local tool = script.Parent
script.Parent.KnockBack.OnServerEvent:Connect(function(player, victim)
if (victim.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude <= 10 and (not player.Character:FindFirstChild("PushForce")) then -- players have to be 3 studs or closer to hit eachother
local PushForce = Instance.new("BodyVelocity")
PushForce.Name = "PushForce"
PushForce.MaxForce = Vector3.new(100000000, 100000000, 100000000) -- amount of knockback force
PushForce.Velocity = (-victim.HumanoidRootPart.CFrame.lookVector) * 100
PushForce.Parent = victim.HumanoidRootPart
wait(0.25)
PushForce:Destroy()
end
end)
-- slash effect when clicking mouse
local function slash()
local str = Instance.new("StringValue")
str.Name = "toolanim"
str.Value = "Slash"
str.Parent = tool
end
script.Parent.KnockBack.OnServerEvent:Connect(function(player, victim)
print("fired")
if (victim.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude <= 10 and (player.Character:FindFirstChild("PushForce") == nil) then -- players have to be 3 studs or closer to hit eachother
print("3 studs apart")
local PushForce = Instance.new("BodyVelocity")
PushForce.Name = "PushForce"
PushForce.MaxForce = Vector3.new(math.huge, math.huge, math.huge) -- amount of knockback force
PushForce.Velocity = (victim.HumanoidRootPart.CFrame.lookVector) * -100
PushForce.Parent = victim.HumanoidRootPart
game:GetService('Debris'):AddItem(PushForce,0.25)
print("ended")
end
end)
-- slash effect when clicking mouse
local function slash()
local str = Instance.new("StringValue")
str.Name = "toolanim"
str.Value = "Slash"
str.Parent = tool
end
I think that works but why I originally thought that my local script was the problem. An animation was suppose to play while it runs the raycast and it wasn’t playing the animation and the knockback wasn’t happening and that’s still the case so I don’t know what’s the problem but I’ve got 2 error messages.
One for
and the error message being KnockBack is not a valid member of Tool
and this error: attempt to index nil with 'Instance'
for
attempt to index nil with “Instance” means that the object does not exist. (or hasn’t loaded in)
KnockBack is not a valid member of Tool - Is KnockBack parented to the Tool?
instead of script.Parent.KnockBack on this line script.Parent.KnockBack.OnServerEvent:Connect(function(player, victim)
try local event = script.Parent:WaitForChild('KnockBack') event.OnServerEvent:Connect(function(player, victim)
Is the remote event named KnockBack? If it’s named something like Knockback or knockback then name it to KnockBack.
The tool is named knockback but like I said this is from a tutorial some parts I might have missed and maybe the remote event is suppose to be named knockback but the script isn’t parented to the remote event anyway so I don’t see the point in naming it. I don’t know what knockback is.
I have a remote event but it’s not named that ,so I renamed the remote event. Should I parent the script to it and change it with what you said, like this part:
Yes, you should. The script should be parented to the TOOL though. Sorry if I was unclear earlier.
The remote event should also be parented to the TOOL. Not the script!
Sorry if what I said was confusing but I meant those are already parented to the tool I’m just saying should the script be parented to the remote event
No, the script should be inside the tool, and the event should be named KnockBack (with the capital letters because Roblox will not recognize the event otherwise)
Yes, replace script.Parent.KnockBack.OnServerEvent:Connect(function(player, victim) with event.OnServerEvent:Connect(function(player, victim) and make local event = script.Parent:WaitForChild(‘KnockBack’) the first line of your script!
So I did all of the above but sadly I still got the error message attempt to index nil with 'Instance'
and the animation I put in the local script still isn’t playing