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

Where to put server scripts that will only work on individual players?

Since only local scripts can be put on server scripts?

Server script(Stamina Regen):

local plr = script.Parent.Parent
plr.MaxStamina.Value = (plr.Stats.Vitality.Value*4)+100
local MaxStam = plr.MaxStamina.Value
db = false

function regenStamina()
	if not db then
		db = true
		while plr.Stamina.Value<MaxStam do
			plr.Stamina.Value = plr.Stamina.Value + (MaxStam/10)
			if plr.Stamina.Value>MaxStam then
				plr.Stamina.Value = MaxStam
			end
			wait(1)
		end
		if plr.Stamina.Value < 0 then
			plr.Stamina.Value = 0
		end
		db = false
	end
end

function addStamina ()
	MaxStam = (plr.Stats.Vitality.Value*4)+100
	plr.MaxStamina.Value = MaxStam
	regenStamina()
end

function giveStamina()
	addStamina()
	plr.Stamina.Value = MaxStam
end

plr.CharacterAdded:Connect(giveStamina)
plr.Stats.Vitality.Changed:Connect(addStamina)
plr.Stamina.Changed:Connect(regenStamina)

2 answers

Use PlayerAdded

game.Players.PlayerAdded:Connect(function(plr) 
    wait()
    plr.MaxStamina.Value = (plr.Stats.Vitality.Value*4) + 100
    local MaxStam = plr.MaxStamina.Value
    local db = false
    —Rest of script
end)

This will work for each individual player, you may have to add a wait(1) or WaitForChild() since the folders and values may not have been added yet.

Script, or ServerScripts, are supposed to handle game mechanics, (for example, in a disasters game, they would handle the disasters). Scripts should always be put into ServerScriptService or the Workspace. On the other hand, LocalScripts are supposed to handle thing with the player, for example health regeneration (or stamina). These scripts are only supposed to go into anything that has Starter in its name (ex. StarterPlayerScripts, StarterGui, StarterPack), with a few exceptions. So your script, which deals with player stamina, should be a LocalScript placed inside of StarterPlayerScripts. also you shoud have something like this:

local plr = game.Players.LocalPlayer

2 comments

0

It was my original script, however the stamina doesnt regen on real game so I had to put it in server script.

0

well then in that case you have to use eigher a single script which handles stamina regen for everyone, or use remote events to communicate to the server. i recomend doing something like herrtt mentioned above

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