I’m trying to make a custom weapon. When it shoots, localscript finds the position of the mouse using uis:GetMouseLocation() and ScreenPointToRay().Origin.
The localscript works fine and it’s prints out the correct vector3 position.
The localscript remotely sends the position to the server script. The server then uses the position it receives to make another ray to find the npc’s instance. I can’t do both rays on the client because not all instances inside the npc loads for the client yet then it can’t find them.
There’s an invisible wall that’s using collisiongroup to prevent he npc from falling off the map while chasing the player. The character model doesn’t collide with the wall so it can go behind that wall (like a safe zone). The player then shoots the npc, but the ray can’t find the npc, instead it’s hitting the wall.
I made a collision group for the ray, but it seems like it’s ignoring it. I’m not sure if my script is right.
Note: The weapon is an accessory with a RightGripAttachment.
The server part:
local tool = script.Parent
local remote = tool:WaitForChild("mouseremote")
local firepart = tool:WaitForChild("FirePart")
local tween = game:GetService("TweenService")
local info = TweenInfo.new(
1,
Enum.EasingStyle.Circular,
Enum.EasingDirection.Out,
0,
false,
0
)
local function maketag(player,clonepart)
local tag = Instance.new("ObjectValue")
tag.Value = player
tag.Name = player.UserId
tag.Parent = clonepart
end
local blacklist = {}
local params = RaycastParams.new()
params.CollisionGroup = "Raycasting"
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = blacklist
local function getray(clonepart,pos,char)
table.insert(blacklist,char)
table.insert(blacklist,workspace.Arena.mobwalls)
local ray = workspace:Raycast(clonepart.Position,pos,params)
if ray then
return ray.Instance
else
clonepart:Destroy()
end
end
remote.OnServerEvent:Connect(function(player,pos)
local clonefire = firepart:Clone()
clonefire.Anchored = true
clonefire.RopeConstraint:Destroy()
clonefire.Parent = workspace
clonefire.Trail.Enabled = true
clonefire.Transparency = 0.9
clonefire.FirePartTouch.Enabled = true
clonefire.CanTouch = true
clonefire.att0.CFrame = CFrame.new(0,2,0)
clonefire.att1.CFrame = CFrame.new(0,-2,0)
maketag(player,clonefire)
local mobInstance = getray(clonefire,pos,player.Character)
if mobInstance then
print("mob found, moving out")
local makemove = tween:Create(clonefire,info,{Position = mobInstance.Position})
makemove:Play()
table.clear(blacklist)
else
print("no mob found")
table.clear(blacklist)
return
end
end)
It’s printing “mob found” when player’s inside the arena and not behind a wall. It’s printing “no mob found” when player goes behind a wall even when the npc is face to face with the player.