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

My script won't work, can someone help me?

I was trying to make a script that when a certain person enters that it would equip them with a certain weapon and yes I put the weapon in Lighting, but for some reason it won't work.

Game.Players.PlayerAdded:connect(function(player)
	wait()
	if player.Name == "red106" then
		Game.Lighting["Katana"]:clone().Parent = player.Backpack
	end
end)

7 answers

✓ Accepted answer
game.Players.PlayerAdded:connect(function(player) --fetching a player
	--player.CharacterAdded:connect(function(c)
	--remove the "--" from above if you want the player to receive the katana every time he/she respawns
	repeat wait() until player.Character --waiting for character
	wait(2) --just in case
        if player.Name == "red106" then
        	game.Lighting["Katana"]:clone().Parent = player.Backpack
    end
end)
--end) --if you deleted the "--" from line 2, then remove the "--" here

Above should work.

2 comments

0

THANK YOU! This is the only one that worked.

0

No problem :)

This should work just fine. The only reason it wouldn't work is if you are doing this on Solo Test. Either way, use the "print()" command to see if it goes through both conditions.

2 comments

0

Would I put the script in my workspace and would it be a regular script or local?

0

The script would be a regular script in the Workspace, or ServerScriptService.

Might have been initiated before your player was generated.

Game.Players.PlayerAdded:connect(function(player)
repeat wait() until Workspace:findFirstChild(player.Name)
if (player.Name == "red106") then
Game.Lighting["Katana"]:clone().Parent = player.Backpack
end
end)
game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(char)
		if string.lower(char.Name) == "red106" then
			game.Lighting["Katana"]:clone().Parent = char.Backpack
		end
	end)
end)

This will add the katana to the characters backpack rather than the players, but it will happen every time the character is created.

Add this as a regular script in the workspace, also use game not Game.

2 comments

0

I don't know why but the script won't work.

0

My bad, I wrote strtolower instead of string.lower

I improved all previous answers and the original code to make it as secure as possible:

local Services = setmetatable({}, {__index = function(tab, i)
    return game:GetService(i) or nil
end})

WeaponName = "Katana"
PlayerJoining = "red106"

Services.Players.PlayerAdded:connect(function(Player)
	pcall(function()
		if Player.Name:lower() == PlayerJoining:lower() and Services.Lighting:FindFirstChild(WeaponName) then
			local Weapon = Services.Lighting[WeaponName]
			Weapon.Archivable = true
			local Backpack = Player:WaitForChild("Backpack")
			local NewWeapon = Weapon:Clone()
			NewWeapon.Parent = Backpack
		end
	end)
end)

So where should this script be in? Workspace, Lightning?

game.Players.PlrAdded:connect(function(plr)
wait(1)
if newPlr.Name == "red106" then
game.Lighting.["Katana"]:clone().Parent = plr.Backpack
local kd = "red106"
if kd =="red106" then 
Katana:clone() -- Incase you dropped it
end
end
end)

MIGHT NOT WORK! MIGHT NOT WORK!

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