hello, im trying to make a simple gun.
but its not shooting.
can you guys help?
i have a localscript, server script, remotevent.
localscript code:
local mouse = game.Players.LocalPlayer:GetMouse()
script.Parent.Activated:Connect(function()
end)
server script code:
script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
local raycastParams = RaycastParams.new()
RaycastParams.FilterDescendantInstances = {player.Character}
RaycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position)*300, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
if model:FindFirstChild("Humanoid") then
model.Humanoid.Health -= 30
end
end
end
end)
remote event name: Fire
Localscript name: Client
server Script name: Server.
Changed Ray cast Origin to the Players PrimaryPart
the reason for this is so when firing the gun at close range, it will actually still detect the Humanoid instead of going though it
script.Parent.Fire.OnServerEvent:Connect(function(Plr,MP)
local RCP = RaycastParams.new()
RCP.FilterDescendantInstances = {Plr.Character}
RCP.FilterType = Enum.RaycastFilterType.Blacklist
local RCR = workspace:Raycast(Plr.Character.PrimaryPart.Position, (MP - Plr.Character.PrimaryPart.Position)*300, raycastParams)
if raycastResult then
local HPart = RCR.Instance
local M = HPart:FindFirstAncestorOfClass("Model")
if M then
if M:FindFirstChild("Humanoid") then
M.Humanoid:TakeDamage(30)
end
end
end
When you initialize variable you make the script read faster and and it is just 1000x more convenient than put the entire way to get the thing !
So he is good !
And you should use variable for the future !
Have a nice day and sorry for my bad english !