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

Is there a way to make a StarterPack failsafe?

I was recently testing a game, and one player spawned without a sword, when the sword was in the StarterPack. I'm trying to create a failsafe in case it happens again. I was going to use a brick. Whenever a player touched that brick, it'd check to see if they had a sword. If they didn't, it'd give them one. Here's the script for the brick... Other than that, I don't know.

local function onTouched(human)
	local tool = game.ReplicatedStorage.LinkedSword
	local player = game.Players:FindFirstChild(human.Parent.Name)
	local h = human.Parent:FindFirstChildWhichIsA("Humanoid")
	if human.Parent:FindFirstChild("LinkedSword") then
		print("This Player has a sword")
	else
		print("This Player has no sword")
		tool:Clone().Parent = player.BackPack
	end
end

script.Parent.Touched:Connect(onTouched)

1 comment

0

If my answer satisfies your question, please accept it.

1 answer

✓ Accepted answer

This is an infinite check (use if you have no other code to execute below this function and you want the player to have a sword at all times):

local function onTouched(human)
    local tool = game.ReplicatedStorage.LinkedSword
    local player = game.Players:FindFirstChild(human.Parent.Name)
    local h = human.Parent:FindFirstChildWhichIsA("Humanoid")
	while true do
	wait(2) --Change the number to how long (in seconds) you want to wait until it checks again.
    	if human.Parent:FindFirstChild("LinkedSword") then
       		print("This Player has a sword")
    	else
        	print("This Player has no sword")
        	tool:Clone().Parent = player.BackPack
		end
	end
end

script.Parent.Touched:Connect(onTouched)

Note: the above script was not edited in its' entirety. It was only edited by adding a "checking" system.

Otherwise, use a short-term check (only edited lines 6 through 15):

local function onTouched(human)
    local tool = game.ReplicatedStorage.LinkedSword
    local player = game.Players:FindFirstChild(human.Parent.Name)
    local h = human.Parent:FindFirstChildWhichIsA("Humanoid")
	local i = 0
	repeat
    	if human.Parent:FindFirstChild("LinkedSword") then
       		print("This Player has a sword")
    	else
        	print("This Player has no sword")
        	tool:Clone().Parent = player.BackPack
		end
		i = i+1
		wait(2) --Change the time to how long you want to wait until it checks again.
    until i == 3 --This script checks for the sword 3 times.
end

script.Parent.Touched:Connect(onTouched)

Note: the above script was not edited in its' entirety. It was only edited by adding a "checking" system.

Edited script for ServerScriptStorage:

function onPlayerAdded(plr)
	local tool = game.ReplicatedStorage.LinkedSword
	while true do
		wait(2)
		if plr.Backpack:FindFirstChild("LinkedSword") or workspace:FindFirstChild(plr.Name):FindFirstChild("LinkedSword") then
			print("This Player has a sword")
		else 
			print("This Player has no sword")
			tool:Clone().Parent = plr.Backpack
		end
	end
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

Note: this script is fully edited and proofread in its' entirety.

11 comments

0

I like that idea. Is there any way where I can implement it in ServerScriptService instead of in a touch brick?

1

You could use the PlayerAdded event. Whenever a player is added into 'game.Players', the function runs for that player.

1

I didn't catch this problem earlier but why are you checking if the sword is within the character? The sword is only within the character when the player selects the tool in their backpack.

1

I added an additional script into my answer for use with ServerScriptStorage

0

It works much better than mine did. The only problem is that when a player pulls their sword out and the script checks to see if they have a sword, it doesn't see a sword, so now they have two swords.

1

Easy fix, just replace line 5 with "if plr.BackPack:FindFirstChild("LinkedSword") or workspace:FindFirstChild(plr.Name):FindFirstChild("LinkedSword") then"

0

Woah, thanks! It worked! :D

0

Sorry, I had put the script in wrong. ;P

1

Great! Happy to hear I solved the problem for you.

0

Turns out, all I did was put the P in Backpack capital instead of lowercase. smh

0

Thanks again!

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