Hi! I have made a script. It works perfectly fine in play solo, but when I do a server test or I upload it to ROBLOX and play it, either an error is returned or it does not work. Firstly, here is the script. I want it to give the player a sword, then teleport them into the map.
local button = script.Parent local player = game.Players.LocalPlayer function teleport() player.Character.HumanoidRootPart.CFrame = CFrame.new(Vector3.new(0,50,0)) end function givetool() game.ServerStorage.Weapons.ClassicSword:Clone().Parent = game.Players.LocalPlayer.Backpack print(player, "has been given a sword") teleport() end button.MouseButton1Click:connect(givetool)
I originally had this in a localscript. When I did play solo, it worked perfectly fine. When I upload it to ROBLOX or I do a server test, it does one of two things:
I'm not sure how I can fix this. I believe I'm trying to mix parts that only work in a local script with parts that only work in a regular script, but I just can't figure this one out. Can anybody provide me with help?
DO NOT use LocalPlayer in a server (regular) script. You must use a LocalScript for it if you want that to work, otherwise it’ll return nil.
Also don’t use ServerStorage when it comes to LocalScripts as I think only server scripts can access ServerStorage, which is probably why it has “Server” in its name.
To fix this, use a LocalScript, put the weapons in ReplicatedStorage as server and local scripts can access it, and make a few changes to your script so it finds the weapons in ReplicatedStorage and not ServerStorage.
If that doesn’t work, please comment about it so I can come back to help. :D
To concatenate in ROBLOX, you would do
print(player .. "has been given a sword")
But there's a problem with this. We're trying to print the player object itself, not the name.
If we take a page from the ROBLOX wiki. http://wiki.roblox.com/index.php?title=API:Class/ServerStorage We can see that LocalScripts can't access ServerStorage in the first place. I sugguest you use something like ReplicatedStorage for your tool storage.
For ease,
game.ServerStorage.Weapons.ClassicSword:Clone().Parent = game.Players.LocalPlayer.Backpack
can be turned into
game.ServerStorage.Weapons.ClassicSword:Clone().Parent = player.Backpack
You're on the right track buddy. Play solo has some extra features that give LocalScripts extra access I am assuming.
Remember how to concatenate, it is very important in string operations. I advise you go onto the wiki and look at some of the top-level singletons like ServerStorage and stuff, stuff like ServerStorage and ServerScriptService cannot be accessed locally, as it can only be accessed by the 'server' (hence Server in the beginning)
LocalPlayer. Hence the 'local', it can only be accessed 'locally' by the 'local' client. You can actually use the names of most of these to find what it can be accessed by.
I hope this helped you!