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

make new instance from tool not damage player wielding it?

ok, so I have a tool that makes an instance spawn. the new instance has a code which then damages all players who touch it. but that's the problem, since it spawns at the position of the player... I think you get the point.

local tool = script.Parent
local mouse
local debounce = false 
local dmg = --set damage here

tool.Equipped:Connect(function(m)
    mouse = m
end)

tool.Activated:Connect(function()
    if mouse then
        local Part = Instance.new("Part",workspace)
        Part.Position = script.parent.Handle.Position
        Part.Material = Enum.Material.Neon
        Part.Size = Vector3.new(0.5,0.5,0.5)
        Part.CanCollide = false
        Part.Anchored = true

    Part.Touched:Connect(function(hit)
        local h = hit.Parent:FindFirstChild("Humanoid")
        if h then
            debounce = true
            h:TakeDamage(dmg)
            wait(1)
        end
        debounce = false
    end)
    end
end)

I know I've been asking a lot of questions here but I just cant find the answer... it would also be appreciated if you could put the link to teach me about this kind of stuff. like in here: https://developer.roblox.com

1 answer

So I fixed it:

local tool = script.Parent
local mouse
local debounce = false 
local dmg = --set damage here
local player

tool.Equipped:Connect(function(m)
    mouse = m
    player = game.Players:GetPlayerFromCharacter(tool.Parent)
end)

tool.Activated:Connect(function()
    if mouse then
        local Part = Instance.new("Part",workspace)
        Part.Position = script.parent.Handle.Position
        Part.Material = Enum.Material.Neon
        Part.Size = Vector3.new(0.5,0.5,0.5)
        Part.CanCollide = false
        Part.Anchored = true

    Part.Touched:Connect(function(hit)
        local h = hit.Parent:FindFirstChild("Humanoid")
        local playerName = player.Name

        if h and not tool.Parent.Name ~= playerName then then
            debounce = true
            h:TakeDamage(dmg)
            wait(1)
        end
        debounce = false
    end)
    end
end)

What I did here, was getting the player using :GetPlayerFromCharacter on tool.Parent. Whenever the Instance is hit, it will check if the hit name is not equal to the player name.

I wasn't able to test it right now, but I hope it helped! You can always put down a comment if it didn't work.

2 comments

0

After making some of my own changes, It worked! at first it didn't but after a couple rounds of trial and error it worked, thanks bud!

0

well, kind of... while testing it didn't hurt the other player but well find a way.

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