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

Script doesn't respawn the player if there is no tool. (?)

Asked by 4 years ago
Edited 4 years ago

I am making a simulator game where they can buy better tools. Sometimes, they don't spawn with the tool for some weird reason. I made this script that detects if they have the tool. When they don't have it, it errors not finding the tool, here is my script.

01game.Players.PlayerAdded:Connect(function(plr)
02plr.CharacterAdded:Connect(function(char)
03wait(1)
04local tool = plr.BackPack.Tool
05If tool then
06print(plr.." has tool")
07else
08char:LoadCharacter()
09print("Respawning".. plr)
10end
11end)
12end)

The script should respawn them if there is no tool, it errors on line 5 when they don't have the tool, if they have the tool it works fine.

I want to thank everybody that spends their time trying to help me, even if they don't know the answer.

Best Wishes, JailBreaker_13

0
it may be because the P in BackPack is capitalized. diggerbear1 32 — 4y
0
I am on mobile right now. It is not the problem. JailBreaker_13 350 — 4y

3 answers

Log in to vote
2
Answered by
Subsz 366 Moderation Voter
4 years ago

Hey there, use FindFirstChild to check if the object exists or not, like so.

01game.Players.PlayerAdded:Connect(function(plr)
02    plr.CharacterAdded:Connect(function(char)
03        wait(1)
04        if plr.Backpack:FindFirstChild('Tool') then
05            print(plr.." has tool")
06        else
07            char:LoadCharacter()
08            print("Respawning".. plr)
09        end
10    end)
11end)
0
Thanks, i will have to try it. I am on mobile right now, i will accept it if it works. JailBreaker_13 350 — 4y
0
It works! The only error i had was it is plr:LoadCharacter, not char:LoadCharacter tysm JailBreaker_13 350 — 4y
Ad
Log in to vote
0
Answered by
quinzt 201 Moderation Voter
4 years ago

youy put a capital i on if

0
Cause im on mobile. It says that it cant find the tool when it is not there. It errors instead of running. JailBreaker_13 350 — 4y
0
you could try ~= nil quinzt 201 — 4y
Log in to vote
0
Answered by 4 years ago

like quinzt said, you put a capital on if,=. I also formatted your code

01game.Players.PlayerAdded:Connect(function(plr)
02    plr.CharacterAdded:Connect(function(char)
03        wait(1)
04        local tool = plr.BackPack.Tool
05        if tool then
06            print(plr.." has tool")
07        else
08            char:LoadCharacter()
09            print("Respawning".. plr)
10        end
11    end)
12end)
0
Cause im on mobile. It says that it cant find the tool when it is not there. It errors instead of running. JailBreaker_13 350 — 4y

Answer this question