What do you want to achieve? Keep it simple and clear!
I’m trying to check if my Bool Value “usingValue” is true before running code that would start the player’s using of the task.
What is the issue? Include screenshots / videos if possible!
Despite printing as true beforehand, the code continues as normal when a second player goes up and interacts, this overlaps the first person and they both use it, despite me only wanting one at a time.
What solutions have you tried so far? Did you look for solutions on the Creator Hub?
I’ve set up prints around the entire script to check that value, and it displays as intended. When someone is on the task, it says true. When nobody is, it says false without fail. I even set up a loop to constantly display that value, and when a person is on it it says true, but when a second person comes up it still makes that second person get on the task.
the usingValue is stored in ReplicatedStorage, and the script this code is running in is a regular script inside the task part. The first time a player gets on task, it works perfectly. But once someone gets off the task and another person gets on, anybody can run up and do it with them.
if usingValue.Value == false then
usingValue.Value = true
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local tasksFolder = ReplicatedStorage.Tasks
local eventsFolder = ReplicatedStorage.Events
local keyPressedEvent = eventsFolder.KeyPressedEvent
local taskObject = script.Parent
local range = taskObject.Range
local taskValues
local usingValue
local completedValue
local progressValue
local rangeTouched = false
local inRangeArray = {}
local function startedTask(plr)
-- create task folder if it doesn't exist
if not tasksFolder:FindFirstChild(taskObject.Name) then
taskValues = Instance.new("Folder")
taskValues.Parent = tasksFolder
taskValues.Name = taskObject.Name
usingValue = Instance.new("BoolValue")
usingValue.Parent = taskValues
usingValue.Name = "Using"
usingValue.Value = false
progressValue = Instance.new("NumberValue")
progressValue.Parent = taskValues
progressValue.Name = "Progress"
progressValue.Value = 0
completedValue = Instance.new("BoolValue")
completedValue.Parent = taskValues
completedValue.Name = "Completed"
completedValue.Value = false
end
-- position player
if usingValue.Value == false then
local playerOnTask = plr
usingValue.Value = true
table.remove(inRangeArray, table.find(inRangeArray, plr))
local position = taskObject.Position
local humrp = plr.Character.HumanoidRootPart
humrp.CFrame = position.CFrame
humrp.CFrame = CFrame.lookAt(humrp.CFrame.Position, taskObject.Object.CFrame.Position)
-- freeze character until move
plr.Character.Humanoid.WalkSpeed = 0
wait(0.5)
keyPressedEvent.OnServerEvent:Connect(function(player, input)
if player == playerOnTask and usingValue.Value == true then
plr.Character.Humanoid.WalkSpeed = 16
usingValue.Value = false
task.wait()
table.insert(inRangeArray, playerOnTask)
end
end)
end
end
range.Touched:Connect(function(hit)
local humanoid = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid")
if humanoid then
local plr = Players:GetPlayerFromCharacter(hit:FindFirstAncestorOfClass("Model"))
if not table.find(inRangeArray, plr) then
table.insert(inRangeArray, plr)
end
end
end)
range.TouchEnded:Connect(function(hit)
local leftHumanoid = hit:FindFirstAncestorOfClass("Model"):FindFirstChild("Humanoid")
if leftHumanoid then
local leftPlr = Players:GetPlayerFromCharacter(hit:FindFirstAncestorOfClass("Model"))
if table.find(inRangeArray, leftPlr) then
table.remove(inRangeArray, table.find(inRangeArray, leftPlr))
end
end
end)
keyPressedEvent.OnServerEvent:Connect(function(plr, input)
if input == "E" then
if table.find(inRangeArray, plr) then
if usingValue then
if usingValue.Value == false then
startedTask(plr)
end
else
startedTask(plr)
end
end
end
end)
while true do
task.wait(1)
if usingValue then
print(inRangeArray)
end
end
here is the code that triggers keyPressedEvent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local keyPressedEvent = ReplicatedStorage.Events.KeyPressedEvent
local MyFunctions = {
["E"] = function(input)
keyPressedEvent:FireServer(input)
end,
["MouseButton1"] = function()
end,
}
UIS.InputBegan:Connect(function(input, chatting)
if chatting then return end
if MyFunctions[input.KeyCode.Name] ~= nil then
task.spawn(MyFunctions[input.KeyCode.Name],input.KeyCode.Name)
end
if MyFunctions[input.UserInputType.Name] ~= nil then
task.spawn(MyFunctions[input.UserInputType.Name])
end
end)
I should add a bit more actually about what im trying to do
basically, I have it to add any players that touch a part added to an array, and players who start working on the task or leave the part unadded, so when you are in the array and press e you start the task
the usingValue is created in the start task function when it’s triggered for the first time, and added to replicatedstorage. when the task starts the player who started it is set to the playerOnTask to avoid confusion since its a server script, they are then teleported to the position and usingValue is set to true.
When E is pressed again, it confirms the player who pressed E is the one on the task, and they are then free to move again, and usingValue is set to false again, it task.waits before adding the player back to the inRangeArray.
I’m doubtful that this is the cause of your current issue, but I suspect it will cause you problems later on. On line 69, you are connecting OnServerEvent within another event connection, and you never disconnect it. This means that it will be running the code within the OnServerEvent connection multiple times, with a different value for playerOnTask. (It runs an additional time each time it is run, for each time that it is connected).
Simplest fix would be to disconnect it, e.g:
local conn
conn = keyPressedEvent.OnServerEvent:Connect(function(player, input)
if player == playerOnTask and usingValue.Value == true then
plr.Character.Humanoid.WalkSpeed = 16
usingValue.Value = false
conn:Disconnect() -------
task.wait()
table.insert(inRangeArray, playerOnTask)
end
end)
it fixed it! thank you so much, funnily enough I learnt about connections yesterday and was like “I gotta remember to use those” but I completely forgot today. thank you!