Sword Giver goes away for everyone

Hey! So I’m having a problem similar to the problem I had yesterday. So basically I want to destroy the sword giver only to the player who got the sword, but it deletes for everyone. This probably is a simple fix but I’m just a noob :sweat_smile:. If you know how to fix this please tell me. Thanks!

swordGiver.Touched:Connect(function(player)
    local humanoid = player.Parent.Humanoid
	humanoid:EquipTool(sword)
	doneChatting = true
	if doneChatting == true then
	     swordGiver:Destroy()
		 dialog.Text = "goodbye :("
	end
			
	if doneChatting == true then
		 yes2.Visible = false
	end
end)

You would put this in a localscript so it wont make changes to the server but the client(You), also you might wanna put that second if statement combine with the first statement

if doneChatting == true then
	     swordGiver:Destroy()
		 dialog.Text = "goodbye :("
         yes2.Visible = false
	end

Also potential issues if executed in this localscript is that an exploiter could change part of this code and make the sword Not destroy by removing the touch event function

Hi, I will try to help you.

First, you can make a value inside a player like this: Instance.new("BoolValue").
Then you’ll need to set the name, parent and value of the bool. You can do this by doing this:

local CanGetSword = Instance.new("BoolValue")
CanGetSword.Name = "CanGetSword"
CanGetSword.Value = true
CanGetSword.Parent = player --You will see about what player am I talking later.

The whole Script will look like this (it should be a Script and you can put it in ServerScriptService, I named the Script SwordValue):

game:GetService("Players").PlayerAdded:Connect(function(player) --Handles player join.
    local CanGetSword = Instance.new("BoolValue")
    CanGetSword.Name = "CanGetSword"
    CanGetSword.Value = true
    CanGetSword.Parent = player
end)

The value Script is done. Now we need to add some changes to the giver Script:

swordGiver.Touched:Connect(function(player)
    if player:WaitForChild("CanGetSword").Value == true then
        local humanoid = player.Parent.Humanoid
	    humanoid:EquipTool(sword)
        swordGiver:Destroy()
        dialog.Text = "goodbye :("
        yes2.Visible = false
    else
        --Notify player here that he already got/ can't get the Sword.
    end
end)

Mark it as a Solution, if it was helpful.

1 Like

on the server:

-- Assuming there is a server script inside of the "Sword Giver" object
local alreadyFired = {
	
}
local sg = script.Parent
local rm = Instance.new("RemoteEvent")
rm.Parent = game.ReplicatedStorage
function getPlayerFromPart(part)
	local maybePlayer = part.Parent.Name
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Name == maybePlayer then
			return v
		end
	end
	return nil
end
sg.Touched:Connect(function(part)
	local player = getPlayerFromPart(part) or false
	if player == false then
		return
	else
		-- give sword then fire client
		if not alreadyFired[player] then
			rm:FireClient(player)
			alreadyFired[player] = true
		else
			return
		end
	end
end)

on the client, in playerscripts

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
 local sg = -- sword giver path
 sg:Destroy()
end)