You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? I want to make a working gun that damages other players.
What is the issue? The bullet hurts the player that owns the gun not the other players even though it touched someone else.
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)
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
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)
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.