Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help at my script please?

Hello, I'm trying to make a tycoon place, personally for mine only for script practice and testing. What I am currently wandering is what type of object should I put my script in. Should I put it in the normal one 'Script' or in the 'LocalScript'? I don't know what's the difference between them to be honest. Here is my script though! If someone could make it simpler, it would be cool. I haven't tested it online though, only 'Play Solo'.

function onPlayerJoin(newPlayer)
local stats = Instance.new("IntValue")
stats.Name = "Leaderstats"
stats.Parent = newPlayer
local cash = Instance.new("IntValue")
cash.Parent = stats
cash.Name = "Cash"
cash.Value = 100
local stats2 = Instance.new("IntValue")
stats2.Name = "Tycoon"
stats2.Parent = newPlayer
end
game.Players.ChildAdded:connect(onPlayerJoin)

1 answer

✓ Accepted answer

The difference between normal scripts (called server scripts) and local scripts is that server scripts run on the server and local scripts run on the client.

Basically, server scripts run on Roblox, while local scripts run on the player's computer. Because of this, local scripts will only run in PlayerGui, Backpack, or the player's character.

You should always use local scripts if you intend to make it a descendant of a player, or inside a character. Server scripts may not work as expected sometimes inside a player.

For your code, it should be a server script inside ServerScriptService or workspace because there is no need to put it inside a player, since you use the ChildAdded event.

Two problems with it though. Instead of ChildAdded, you should really use the PlayerAdded event. Second, 'leaderstats' should always be spelled with a lower case L, assuming you want it to appear on the leaderboard.

3 comments

0

What's the problem between ChildAdded and PlayerAdded event?

0

PlayerAdded events do not work well in local scripts, so in them you should use ChildAdded. However, if you're using a server script you should use PlayerAdded because it is built specifically for PLAYERS joining the game, while ChildAdded just fires every time an instance is added to the object it is used on.

0

Alright, thanks. Very helpful

This discussion is preserved as it was. New answers, comments, and votes are closed.