Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Touch and Touchended events when player is on the edge?

Asked by 4 years ago

I'm trying to make a sword arena script where if you are in a certain area then you get a sword and if you leave that area the sword is removed

Possible ways I could have done this: 1. Check if a player is in a certain X, Y, Z 2. Check if a player is in a region using region3 3. Check if a player has touched a brick

I used option 3 and have the below code

opened = false
local check = game.Workspace.check
local sword = game.ReplicatedStorage.Heal

check.Touched:connect(function(hit)
    if opened then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if hit.Parent:FindFirstChild("Humanoid") then
            char = plr.Character
            if not hit.Parent:FindFirstChild("Heal") then
                if not game.Players[hit.Parent.Name].Backpack:FindFirstChild(sword.Name) then
                    sword:Clone().Parent = hit.Parent
                    wait(5)
                end
            end
        end
    end 
end)

function CheckIfPlayerIsInArea(check,Character)
    local touching = check:GetTouchingParts()
    for i=1,#touching do
        if touching[i] == Character.HumanoidRootPart then
            return true
        end
    end
    return false
end

check.TouchEnded:connect(function(remove)
    if remove.Parent:FindFirstChild("Humanoid") then
        local plr = game.Players:GetPlayerFromCharacter(remove.Parent)
        local char =  plr.Character
        if CheckIfPlayerIsInArea(check, char) == false then
            if plr.Backpack:FindFirstChild("Heal") then
                plr.Backpack.Heal:Destroy()
            elseif plr.Character:FindFirstChild("Heal") then
                char.Heal:Destroy()
            end
        end
    end
 end)

The problem is if the user is on the edge it spam gives them the sword making the annoying unsheath sound for swords.

I've tried adding a wait() to both remove and give functions but it didn't seem to do much.

How do I get around this as I don't want players spamming their sword and is there a better way to script this?

Thanks in advance!

1 answer

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago

You could add a debounce

local debounce =  true

opened = false
local check = game.Workspace.check
local sword = game.ReplicatedStorage.Heal

check.Touched:connect(function(hit)
    if opened then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        if hit.Parent:FindFirstChild("Humanoid") then
    if debounce then
        debounce = false
        wait(1)

            char = plr.Character
            if not hit.Parent:FindFirstChild("Heal") then

                if not game.Players[hit.Parent.Name].Backpack:FindFirstChild(sword.Name) then
                    sword:Clone().Parent = hit.Parent
                    wait(5)
                      debounce = true
                    end
                end
            end
        end
    end 
end)

function CheckIfPlayerIsInArea(check,Character)
    local touching = check:GetTouchingParts()
    for i=1,#touching do
        if touching[i] == Character.HumanoidRootPart then
            return true
        end
    end
    return false
end

check.TouchEnded:connect(function(remove)
    if remove.Parent:FindFirstChild("Humanoid") then
        local plr = game.Players:GetPlayerFromCharacter(remove.Parent)
        local char =  plr.Character
        if CheckIfPlayerIsInArea(check, char) == false then
            if plr.Backpack:FindFirstChild("Heal") then
                plr.Backpack.Heal:Destroy()
            elseif plr.Character:FindFirstChild("Heal") then
                char.Heal:Destroy()
            end
        end
    end
 end)
0
That's a good idea but the problem still persists. I think its how I set up the touch and touchended functions. If the user is on the edge its bound to just remove and add the tool. I'll try to use inequalities to see if the character is in a certain X, Y, Z instead LordLightingXII 12 — 4y
Ad

Answer this question