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

Scope Issues, not changing variable?

Hello.

I have a script that changes the variable "turn" to true. The variable "turn" changes true at line 76 but doesn't fire the conditional statement at line 49. I think its due to the variable "turn" scope inside the function.

Thank you.

Full Code:

local replicatedStorge = game:GetService("ReplicatedStorage")

local runService = game:GetService("RunService")


local players = game:GetService("Players")
local turnpart = game.Workspace.TurnTo


local maxTime = 500
local regen = 5
local cost = 5

local turnTime


local turnTable = {}


players.PlayerAdded:Connect(function(player)
	local turnTime = Instance.new("IntValue",player)
	turnTime.Value = 500
	turnTime.Name = "Turn"
	
	turnTime.Changed:Connect(function(property)
		replicatedStorge.Remotes.ResetUpdate:FireClient(player,turnTime.Value, maxTime)
	end)
end)


local turn = false
local gyro 
local seat


replicatedStorge.Remotes.LowerBar.OnServerEvent:Connect(function(player,state,seat)
	seat = seat
	local character = player.Character
	gyro = seat.BodyGyro
	
	if state == "Begin" then
		turnTable[player.Name] = true
	elseif state == "Ended" and turnTable[player.Name] then
		turnTable[player.Name] = nil
	end
end)


if turn then
	gyro.MaxTorque = Vector3.new(99999999999,999999,9999999999)
	gyro.CFrame = CFrame.new(seat.Position, turnpart.Position)
	gyro.P = 7000
end 





runService.Heartbeat:Connect(function()
		for index, players in pairs(players:GetChildren())do
			turnTime = players.Turn
			local name = players.Name
			
			if not turnTable[name] then
				if turnTime.Value > maxTime then
				turnTime.Value = maxTime
				elseif turnTime.Value < maxTime then
				turnTime.Value = turnTime.Value + regen
				end
			else 
				if turnTime.Value >= cost then
					turnTime.Value = turnTime.Value - cost
				else 
				turnTable[name] = nil
					print("firing")
					turn = true
					wait(2)
					turn = false
				end
				end
			end
	end)

3 comments

0

Output ?

0

It prints "firing" just dosent run.

0

Could you tidy up your code? Just go to https://goonlinetools.com/lua-beautifier/

1 answer

This is because you forgot to put that code segment inside of the event. Currently, it's only being checked once. Try placing that conditional statement inside LowerBar.OnServerEvent, and see how it turns out.

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