Gun Not Working Properly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to make a working gun that damages other players.

  2. What is the issue? The bullet hurts the player that owns the gun not the other players even though it touched someone else.

  3. What solutions have you tried so far? Youtube.

Here is my script



script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,mouse)
	


	local mouse = mouse
	local Bullet = game.ReplicatedStorage.Bullet

	local clones = Bullet:Clone()
	clones.Parent = script.Parent
	clones.Position = script.Parent.Barrel.Position
	wait(0.0000001)
	local tweenservice = game:GetService("TweenService")
	local Time = 0.5
	local tweenInfo = TweenInfo.new(
		Time,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.In,
		0,
		false,
		0
	)

	local PosX, PosY,PosZ = mouse.X,mouse.Y,mouse.Z


	game.Workspace.gunshot:Play()	
	local properties = {Position = Vector3.new(PosX,PosY,PosZ)}

	local tween = tweenservice:Create(clones,tweenInfo,properties)

	tween:Play()
	local debounce = true
	local timebetdamage = 0.5

clones.Touched:Connect(function(hit) --- This is the damage player script

		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce == true then
				debounce = false
				hit.Parent.Humanoid:TakeDamage(5)
				wait(timebetdamage)
				debounce = true
			end
		end

	end)

	wait(0.30)
	clones:Destroy()





end)
1 Like

You just need to check if the game:GetService(“Players”):GetPlayerFromCharacter(hit.Parent) is the same as the player variable in your remote event OSE connection

1 Like

How can I do that? (30Charact)

As I said in the original reply, you can use game:GetService("Players"):GetPlayerFromCharacter(characterModel) but replace characterModel with hit.Parent and just check if the result from that code piece is the same as the player variable.
(edit: Players | Roblox Creator Documentation)

local repstorage = game:GetService("ReplicatedStorage")

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,mouse)
	local bullet = repstorage:WaitForChild("Bullet")
	local bulletclone = bullet:Clone()
	bulletclone.Parent = script.Parent
	bulletclone.Position = script.Parent.Barrel.Position
	task.wait()
	local tweenservice = game:GetService("TweenService")
	local timed = 0.5
	local tweenInfo = TweenInfo.new(
		timed,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.In,
		0,
		false,
		0
	)
	local PosX, PosY,PosZ = mouse.X,mouse.Y,mouse.Z
	game.Workspace:WaitForChild("gunshot"):Play()	
	local properties = {Position = Vector3.new(PosX,PosY,PosZ)}
	local tween = tweenservice:Create(bulletclone,tweenInfo,properties)
	tween:Play()
	local debounce = true
	local timebetdamage = 0.5
	bulletclone.Touched:Connect(function(hit) --- This is the damage player script
		if hit.Parent:FindFirstChild("HumanoidRootPart") then
			if debounce then
				debounce = false
				hit.Parent.Humanoid:TakeDamage(5)
				task.wait(timebetdamage)
				debounce = true
			end
		end
	end)
	task.wait(0.3)
	bulletclone:Destroy()
end)

Where’s the local script?

1 Like

In the pistol with the server script

You need a local script to call FireServer() for that OnServerEvent event to be fired.

I did, the problem is the bullet it damaging the player that owns the gun not anyone else

Just a quick little tip, use task.wait instead of wait.

Also the time set on that wait is limited to a 30 hertz clock, that means it has a minimum wait time of 0.0333 seconds. This was when roblox ran at a 30 fps cap but those days are long over now. (task.wait is the same but has a minimum time of 0.01666 seconds).

Also if you don’t add any arguments to wait/task.wait, it will use the shortest time possible.

Can you share the local script?

Sure

local mouse = game.Players.LocalPlayer:GetMouse()
	script.Parent.RemoteEvent:FireServer(mouse.Hit)
end)

I suggest having the RemoteEvent stored in replicated storage, and then referencing it like this:

local repstorage = game:GetService("ReplicatedStorage")
local remote = repstorage:WaitForChild("RemoteEvent")

As opposed to having the remote inside the gun model/tool itself.

Also that 2nd script has an extra end statement & bracket at the end which isn’t needed.

local mouse = game.Players.LocalPlayer:GetMouse()
script.Parent.RemoteEvent:FireServer(mouse.Hit)

Havent been able to do it think you can add it to the script?