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

Super Saiyan Script works fine in studio but when it's in game it just doesn't work at all?

Asked by 6 years ago

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)    

Tree

0
connect is deprecated, use Connect green271 635 — 6y
0
Changed :connect to :Connect but it still doesn't work LoganboyInCO 150 — 6y
0
its because its decaperated User#23365 30 — 6y

2 answers

Log in to vote
3
Answered by 6 years ago
Edited 6 years ago

Ancestry

The top ancestor (excluding the DataModel and the Players service) of a LocalScript is, well, the LocalPlayer. And LocalScripts run for one client, you. Scripts run on the server, which is responsible for connecting all the players together in your game.

Why LocalPlayer is nil on the server

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'.

1
Oof, green got here and helped me fix it before you did but appreciated for your support. LoganboyInCO 150 — 6y
0
i'd appreciate it if you accepted this answer though, not mine green271 635 — 6y
0
who got here first not me User#19524 175 — 6y
0
i don't see why he shouldn't accept the better answer though, yours goes more indepth then mine green271 635 — 6y
0
seems to..... LoganboyInCO 150 — 6y
Ad
Log in to vote
2
Answered by
green271 635 Moderation Voter
6 years ago
Edited 6 years ago

Server Scripts and LocalPlayer

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 vs Connect

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)    

0
Thanks, I should really start smashing :Connect in my head and not use LocalPlayer. LoganboyInCO 150 — 6y

Answer this question