Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problem with the remote event. Any solutions?

Hello. I was trying to build and code a gun. Here's the code.

Local script

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local shoot = game:GetService("ReplicatedStorage"):WaitForChild("Sparo")
local tool = script.Parent
local hole = script.Parent:WaitForChild("Buco")

tool.Equipped:connect(function()
	mouse.Button1Down:connect(function()
		local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p)* 500)
		local hit, position = workspace:FindPartOnRay(ray, player.Character)
		if hit then
	    local humanoid = hit.Parent:FindFirstChild("Humanoid")
		shoot:FireServer(hit, humanoid)
		end
	end)
end)

Server script

local shoot = game:GetService("ReplicatedStorage"):WaitForChild("Shoot")
local damage = game.StarterPack.Tool.Configuration.Danno.Value

shoot.OnServerEvent:Connect(function(hit, humanoid)
		if hit.Name == "Head" then
		print("Headshot")
		humanoid:TakeDamage(damage * 2) 
		else
		print("Hit")
		humanoid:TakeDamage(damage)
		end
end)

The output

Hit
12:03:46.469 - TakeDamage is not a valid member of Part

I can't get it to detect headshots and deal damage.

I tryied to run all the code on a local script and it worked fine. But when I try to use remote events the arguments start working in a weird way. I'm still learning and I would appreciate an explanation of the arguments. Any solution to this?

Thanks in advance.

1 answer

✓ Accepted answer

everybody has had this problem at least once. the player is always the first parameter then the first argument becomes second parameter and stuff so

local shoot = game:GetService("ReplicatedStorage"):WaitForChild("Shoot")
local damage = game.StarterPack.Tool.Configuration.Danno.Value

shoot.OnServerEvent:Connect(function(player, hit, humanoid)
        if hit.Name == "Head" then
        print("Headshot")
        humanoid:TakeDamage(damage * 2) 
        else
        print("Hit")
        humanoid:TakeDamage(damage)
        end
end)

Sorry abuot that, keep the player parameter tho. The problem is that ur just using FindFirstChild and not putting and if then statement to check it so.

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local shoot = game:GetService("ReplicatedStorage"):WaitForChild("Sparo")
local tool = script.Parent
local hole = script.Parent:WaitForChild("Buco")

tool.Equipped:connect(function()
    mouse.Button1Down:connect(function()
        local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p)* 500)
        local hit, position = workspace:FindPartOnRay(ray, player.Character)
        if hit then
    		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then -- if then to check if its nil
    			shoot:FireServer(hit, humanoid)
       		end
	end
    end)
end)

1 comment

0

Thanks for the explanation.

This discussion is preserved as it was. New answers, comments, and votes are closed.