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

How do I fix this code to cause a value to execute code when the game starts?

I'm new to scripting so some of this might be just really dumb mistakes. So far I had a script that when a player clicked a part then the switch was on and an explosion was created. Now I am trying to make it so that this part is "clicked" or turned on right as the first player joins. I have gotten this far into the code and I'm not sure what to do next.


sp=script.Parent

for _, player in pairs(game.Players:GetChildren()) do
		sp.LightSwitch:SetValue(1)
		sp.SwitchWeld.C1=CFrame.Angles(.8,0,0)
end 


1 answer

✓ Accepted answer
local sp = script.Parent
local first = false -- Debounce to keep it from firing after first player
game.Players.PlayerAdded:connect(function(plyr) --- Event that fires when a player joins
	repeat wait() until plyr.Character -- Waiting for player's character
	if (first == false) then first = true -- Checking if the player is the first player, sets debounce to true
		sp.LightSwitch:SetValue(1)
		sp.SwitchWeld.C1=CFrame.Angles(.8,0,0)
	end
end) -- Anonymous functions (unnamed functions) need a closing parentheses on its end

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