I have a game that has a localscript that has two tasks: 1 display the time the game has been running for the player and 2 detect if the player is near a specific object.
The script suddenly stopped working with the task 2 not happening.
I have commented out all the code except for the basic print the player position but this prints out the position of the player when it was on the spawnpoint. I copied this specific task code to a new project and it works ok there so I am concluding that something in my game is causing this failure.
My question is how do I go about finding out which part of the game is causing this situation to occur.
My current approach is to disable scripts one at a time and see if this local script works again.
Is there any other aproach I should consdier?
Where is this local script running at? If it is running in StarterPlayerScripts and the Character loads after the task it won’t work. If this is not the case can I take a look at the script?
print(“Shop!”)
local Players = game:GetService(“Players”)
local LP = Players.LocalPlayer
–[[
local PID = tostring(LP.UserId)
local PGui = LP:WaitForChild(“PlayerGui”)
local ShopGui = PGui:WaitForChild(“ShopGui”)
local frame = ShopGui:WaitForChild(“Frame”)
local PN = LP.Name
local ActivePlayer = workspace:WaitForChild(PN)
local RS = game:GetService(“ReplicatedStorage”)
local GetOresValue = RS:WaitForChild(“GetOresValue”)
local myPosition = game.Workspace.SHOP.PrimaryPart.Position
]]
local char = LP.Character or LP.CharacterAdded:wait()
local humanoidRootPart = char:WaitForChild(“HumanoidRootPart”)
while humanoidRootPart do
print(“player pos=”,humanoidRootPart.Position)
wait(1)
end
–[[
function CheckSetShopGuis()
-- if no ores then make Sell not visible
if GetOresValue:InvokeServer() == 0 then
ShopGui.Frame.Sell.Visible = false
else
ShopGui.Frame.Sell.Visible = true
end
end
while 1==1 do
local playerPosition = humanoidRootPart.Position
print(“Shop: player Pos”, playerPosition)–, " shop pos=",myPosition,“delta=”,(playerPosition-myPosition) ,tick())
local distance = (playerPosition - myPosition).magnitude
–print(“Shop:>”, distance, tick())
if distance < 15 then
CheckSetShopGuis()
ShopGui.Enabled = true
ShopGui.Frame.ShopStatus.Text = "OPEN"
ShopGui.Frame.ShopStatus.Visible = true
while ShopGui.Enabled == true do
playerPosition = ActivePlayer.PrimaryPart.Position
distance = (playerPosition - myPosition).magnitude
if distance > 16 then
ShopGui.Frame.ShopStatus.Text = "CLOSED"
wait(1) -- allow player to see
ShopGui.Frame.ShopStatus.Visible = false
ShopGui.Enabled = false
end
wait(2)
--print("Shop:=", distance, tick())
end
else if distance > 16 then
– ShopGui.Frame.ShopStatus.Text = “CLOSED”
– wait(1) – allow player to see
– ShopGui.Frame.ShopStatus.Visible = false
ShopGui.Enabled = false
end
end
wait(1)
–print(“Shop:<”, distance, tick())
– wait(1)
end
]]
This was a pain to replicate, please use the preformatted text format next. You should also consider using task.wait()
instead of wait
since it is deprecated. Because of the poor formatting I decided to remake it entirely, the issue was likely caused by your “shop” model / part streaming out.
local Players = game:GetService("Players")
local LP = Players.LocalPlayer
local PID = tostring(LP.UserId)
local PGui = LP:WaitForChild("PlayerGui")
local ShopGui = PGui:WaitForChild("ShopGui")
local frame = ShopGui:WaitForChild("Frame")
local PN = LP.Name
local ActivePlayer = workspace:WaitForChild(PN)
local RS = game:GetService("ReplicatedStorage")
local GetOresValue = RS:WaitForChild("GetOresValue")
local myPosition = workspace:WaitForChild("Shop").PrimaryPart.Position
repeat task.wait() until LP.Character
local char = LP.Character
local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
--[[ while humanoidRootPart do
wait(1)
end
What is the purpose of this?
--]]
local ShopFrame = ShopGui:WaitForChild("Frame")
function CheckSetShopGuis()
if GetOresValue:InvokeServer() == 0 then
ShopFrame:WaitForChild("Sell").Visible = false
else
ShopFrame:WaitForChild("Sell").Visible = true
end
end
while true do -- instead of 1 == 1
local playerPosition = humanoidRootPart.Position
local distance = (playerPosition - myPosition).magnitude
if distance <= 15 then -- equal or smaller than 15
CheckSetShopGuis()
ShopGui.Enabled = true
ShopFrame:WaitForChild("ShopStatus").Text = "OPEN"
ShopFrame.ShopStatus.Visible = true
print("Shop:=", distance, tick())
--[[ while ShopGui.Enabled == true do
playerPosition = ActivePlayer.PrimaryPart.Position
distance = (playerPosition - myPosition).magnitude
if distance > 16 then
ShopFrame.ShopStatus.Text = "CLOSED"
task.wait(1) -- allow player to see
ShopFrame.ShopStatus.Visible = false
ShopGui.Enabled = false
end
task.wait(2)
end
There is no need for this as the while loop above also does the same thing, this will also cause traffic and potentially lead up to infinite loops.
--]]
else -- no need for distance > 16 because it will always be bigger than that.
ShopGui.Frame.ShopStatus.Text = "CLOSED"
task.wait(1)
ShopGui.Frame.ShopStatus.Visible = false
ShopGui.Enabled = false
end
task.wait(2)
end
local Players = game:GetService("Players")
local LP = Players.LocalPlayer
local PID = tostring(LP.UserId)
local PGui = LP:WaitForChild("PlayerGui")
local ShopGui = PGui:WaitForChild("ShopGui")
local frame = ShopGui:WaitForChild("Frame")
local shopStatus = frame:WaitForChild("ShopStatus")
local PN = LP.Name
local ActivePlayer = workspace:WaitForChild(PN)
local RS = game:GetService("ReplicatedStorage")
local myPosition = workspace:WaitForChild("SHOP").Torso.Position
repeat task.wait() until LP.Character
local char = LP.Character
local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
local ShopFrame = ShopGui:WaitForChild("Frame")
while true do
local playerPosition = humanoidRootPart.Position
print("Shop: playerPosition=", playerPosition, tick())
local distance = (playerPosition - myPosition).magnitude
print("Shop: distance=", distance, tick())
if distance <= 15 then -- equal or smaller than 15
ShopGui.Enabled = true
ShopFrame:WaitForChild("ShopStatus").Text = "OPEN"
ShopFrame.ShopStatus.Visible = true
else
shopStatus.Text = "CLOSED"
task.wait(1)
shopStatus.Visible = false
ShopGui.Enabled = false
end
task.wait(2)
end
above is the code.
It does not work in my game.
It does work in my testplace which is here:
TestPlace7.rbxl (82.4 KB)
I tested the place and it does open / close the shop. What seems to be the issue you are referring to?
In my game the same code does not work.
The print of playerPosition remains the same no matter where the player is.
I see, have you modified the character in any way? If not try downloading your own place and see if it works on a different file. Lastly try if it works on Roblox, if it does then it is likely a studio corruption / glitch.
I have the game in my development area and the character is standard model.
The problem exists when playing in Roblox player.
Could not understand, can you clarify if it works on Roblox or Roblox studio.
The game fails on both. The testcase works in studio.
If this script works, and it does in testing your script, then it’s not the issue. What else have you changed? What other scripts interact with the player? This is most likely interference with the LocalPlayer.Character or the HumanoidRootPart updates in your game.
Thank you for the hint. My approach to find the cause is to save a copy of the current version, revert the version backwards until this script works again, compare the failing one with the good one and identify what changes had been made to cause the problem.