I was about to finish the work on this game, it was pretty much all done and dusted until I decided that I should have a bit of a "Dev perk" which gives me a pistol. Here's the LocalScript in Workspace:
if game.Players.LocalPlayer.Name == "MrMoob_YT" then local pistol = game.ReplicatedStorage.Pistol:Clone() while true do pistol.Parent = game.Players.MrMoob_YT.Backpack wait() end end
The script doesn't give the pistol, and it doesn't give any error message. What's wrong?
Local scripts do not run in Workspace
, from official Roblox page:
A LocalScript will only run Lua code if it is a descendant of one of the following objects:
- A Player’s Backpack, such as a child of a Tool
- A Player’s character model
- A Player’s PlayerGui
- A Player’s PlayerScripts.
- The ReplicatedFirst service
Thus you could put the script into StarterPlayerScripts
, however this is no good too, if you locally clone the tool and the tool has server scripts in it, they won't run, this is made to prevent exploiters. Instead, create a script in ServerScriptService
and an event listening to PlayerAdded
:
-- GetService is a good practice, you don't have to use it, the difference -- is that if service you are trying to get does not exist, using game. will -- throw an error, GetService will create a new one instead, this does not -- happen to services like Players because they are created by roblox -- scripts since they require them, you might see this with DataStoreService -- Also if you rename the service, game. will error while GetService will -- always return it local ReplicatedStorage = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") -- Your user id, if you ever change name, you will -- need to change the script, id will always stay the same local USER_ID = 1446387420 Players.PlayerAdded:Connect(function(player) --if player.Name == "MrNoob_YT" then if player.UserId == USER_ID then player.CharacterAdded:Connect(function() local pistol = ReplicatedStorage.Pistol:Clone() pistol.Parent = player:WaitForChild("Backpack") end) end end)
You made a loop which gives the player the tool, however you can instead use CharacterAdded event which fires every time you respawn.