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

My script does not work in-game or in a server test, but "play solo" works fine?

Asked by 6 years ago

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:

  • If it is in a localscript, the output shows a blank message.
  • If it is in a regular script, the output shows that it is it's trying to index a nil value (LocalPlayer)

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?

2 answers

Log in to vote
1
Answered by 6 years ago

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

0
Thanks so much! You explained everything clearly and what you suggested work. MrBlockyhead 84 — 6y
Ad
Log in to vote
0
Answered by
Robin5D 186
6 years ago

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!

0
Thank you so much also! Unfortunately, I can only accept one answer, but you were too a great help! MrBlockyhead 84 — 6y

Answer this question