I created a chat command script that makes you able to go from super saiyan to super saiyan blue and it works fine in studio but in game it just doesn't work
Do I need to change anything or do I need to use local scripts?
Super saiyan chat command
game.Players.PlayerAdded:connect(function(p) p.Chatted:connect(function(msg) msg = msg:lower() if (msg == "ssj") then if (p.Character ~= nil) then game.Players.LocalPlayer.Character.gokuhair.ssj.Disabled = false end end end) end)
Super saiyan god chat command
game.Players.PlayerAdded:connect(function(p) p.Chatted:connect(function(msg) msg = msg:lower() if (msg == "ssg") then if (p.Character ~= nil) then game.Players.LocalPlayer.Character.gokuhair.ssg.Disabled = false end end end) end)
Super saiyan blue chat command
game.Players.PlayerAdded:connect(function(p) p.Chatted:connect(function(msg) msg = msg:lower() if (msg == "ssgss") then if (p.Character ~= nil) then game.Players.LocalPlayer.Character.gokuhair.ssgss.Disabled = false end end end) end)
The top ancestor (excluding the DataModel
and the Players
service) of a LocalScript
is, well, the LocalPlayer
. And LocalScript
s run for one client, you. Script
s run on the server, which is responsible for connecting all the players together in your game.
Think of it as your school classmates and your teacher. Think of your school classmates as other clients, and think of your teacher as the server. If you told your teacher to get the local student, your teacher would be very confused, because which student is the local student? For this reason, LocalPlayer
is nil on the server. Your game could have multiple players in it, and when you try using LocalPlayer
, it can't just randomly pick a player. It makes no sense. Code cannot read your mind. This is why it must be all typed out. So instead of doing this:
make a part inside of the workspace with color red
You must do this:
local part = Instance.new("Part") part.BrickColor = BrickColor.new("Really red") part.Parent = workspace
With that in mind, let's fix up your script:
game.Players.PlayerAdded:Connect(function(p) p.Chatted:Connect(function(msg) msg = msg:lower() if msg == "ssj" and p.Character then p.Character.gokuhair.ssj.Disabled = false end end) end)
Pretty much for each time you say game.Players.LocalPlayer
, you'd say p
instead. PlayerAdded
passes the player who joined, making this a whole lot easier.
The reason your script "worked" in Studio was because in Studio's Play Solo mode, there is no separation of the server and client. In order to get accurate results whilst using Play Solo, go to Settings>Studio>Use Accurate Play Solo
You can also use local servers to simulate a real game.
And finally, RBXScriptSignal:connect()
with a lowercase 'c' is deprecated, use RBXScriptSignal:Connect()
with an uppercase 'C'.
The server cannot use LocalPlayer
. It will always return nil
when used. This only applies to ingame, which is why it only works in studio. Luckily for you, you already have a player variable setup thanks to PlayerAdded
!
connect
is deprecated. Please use Connect
instead.
Updated code:
Super Saiyan chat command
game.Players.PlayerAdded:connect(function(p) p.Chatted:Connect(function(msg) msg = msg:lower() if (msg == "ssj") then if (p.Character ~= nil) then p.Character.gokuhair.ssj.Disabled = false end end end) end)
Super Saiyan god command
game.Players.PlayerAdded:Connect(function(p) p.Chatted:Connect(function(msg) msg = msg:lower() if (msg == "ssg") then if (p.Character ~= nil) then p.Character.gokuhair.ssg.Disabled = false end end end) end)
Super Saiyan blue chat command
game.Players.PlayerAdded:Connect(function(p) p.Chatted:Connect(function(msg) msg = msg:lower() if (msg == "ssgss") then if (p.Character ~= nil) then p.Character.gokuhair.ssgss.Disabled = false end end end) end)