Scripts not syncing? (HELP)

Hello! I am very new when it comes to any kind of scripting, so I apologize in advance if I sound stupid and none of you guys know what I am talking about and if I am too vague. As of lately I have encountered a problem whilst working with someone on a project for a bank robbery system. When we finally finished and imported what we had made into our main game it would for some odd reason not work as intended compared to our original test place. I compared both games with almost everything and they still seem to not work and behave differently despite there being no differences whatsoever. When looking at the outputs, they seem to give off different results however.

What is supposed to happen in the original version is that you walk through the “vault door” into a room that is supposed to give you approximately 100 Cash once you touch the stack of money.
image
Once you equip the cash, you must walk through the same “Vault door” which is the slightly transparent brick, and then once you go through the door you must walk 550 studs away from the vault door for it to award you the cash in the leaderboard.


Looking at the output, it shows how much studs you have walked so far from the vault door, which looks like this:
image
Now, let me get to the root of the problem! For some reason when I test it in the main game, right when I walk through the door once I equip the cash, instead of it waiting for me to walk 550 studs away from the door, it immediately awards me the cash. I noticed that when I look at the output, it is much different and doesn’t count the amount of steps I have walked from the vault door, but instead the approximate position of the vault door itself. Here is what that looks like in the output:
image
As you can see, its just one small thing. When I went to the source of it, apparently the script in the “vault door” from line 45 and down have something to do with it. Here is a screenshot:

So, I am not completely sure what to do, and I had a friend work on this and he is dumbfounded as to why it is not working when I inserted everything from our test game into my main game, but I hope one of you out there can hopefully decipher this because I honestly do not know what to do nor what I am looking at, the only thing I could think of is that it is not syncing or something, but I have no idea.

Oh, and as requested, here is the full code:

local disTanceText2 = 0

local function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if script.Parent:FindFirstChild(hit.Parent.Name) then
			
		return end
		if script.Parent.Parent.Bills.Bank.Value == true then
			
		return end
		local Player = game.Players:FindFirstChild(hit.Parent.Name)
		playerName = hit.Parent.Name
		local part = Instance.new("Part")
		part.Anchored = true
		part.Parent = script.Parent
		part.Name = hit.Parent.Name
		part.Position = script.Parent.Position
		part.CanCollide = false
		part.Transparency = 1
		script.Parent.Parent.Bills.Bank.Value = true
	end
	showGui()
end

function showGui()
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
			if player.Team == game:GetService("Teams").Police then
			local clone = game.ReplicatedStorage.BankRob:Clone()
			clone.Parent = player.PlayerGui
		end
	end
	distanceCheck()
end

function distanceCheck()
	repeat 
		local Player = game.Players:FindFirstChild(playerName)
		if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
			local disTance = Player:DistanceFromCharacter(Vector3.new(game.Workspace.Monet.VaultEnter:FindFirstChild(playerName).Position))
			local disTanceText1 = math.floor(disTance)	
			disTanceText2 = disTanceText1
		end
			wait(0.1)
			print(disTanceText2)
	until disTanceText2 > 550
	endFunction()
end

function endFunction()
	if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
		local part = game.Workspace.Monet.VaultEnter:FindFirstChild(playerName)
		part:Destroy()
	end
	disTanceText2 = 0
	local Player = game.Players:FindFirstChild(playerName)
	Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + Player.robInfo.cashTaken.Value
	Player.robInfo.cashTaken.Value = 0
	Player.PlayerGui.BankGui.Main.Visible = false
	script.Parent.Parent.Bills.CoolDown.CoolDownStart:FireClient(Player)
	script.Parent.Parent.Bills.Bank.Value = false
	script.Parent.Script.Disabled = true
end

script.Parent.Touched:Connect(onTouch)

UPDATE:
So we have decided to change it up a little bit and we are now making it so when you take the Cash, instead of walking 550 studs away you must instead drop off the cash to be awarded it in the leader stats. The new problem we are having now is that once you walk up to the brick that awards you the cash at the drop off, it is not awarding you the cash nor getting rid of the GUI that says how much cash you have. Here is the updated code:

local disTanceText2 = 0

local function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        
        if script.Parent:FindFirstChild(hit.Parent.Name) then
            
        return end
        
        local Player = game.Players:FindFirstChild(hit.Parent.Name)
        
        game.ReplicatedStorage.NameCheck:FireClient(Player)
        
        game.ReplicatedStorage.NameCheck.OnServerEvent:Connect(function(Player, Name)
            playerName = Name
            print(playerName .. Name)
        end)
        
        playerName = hit.Parent.Name
        local part = Instance.new("Part")
        part.Parent = script.Parent
        print(playerName)
        part.Name = hit.Parent.Name
        part.Position = script.Parent.Position
        part.Transparency = 1
        script.Parent.Parent.Bills.Bank.Value = true
    end
    
    showGui()
end

function showGui()
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
            if player.Team == game:GetService("Teams").Police then
            local clone = game.ReplicatedStorage.BankRob:Clone()
            clone.Parent = player.PlayerGui
        end
    end
end

function robberCheck(hit)
    if hit.Parent.Name == playerName then
        endFunction()        
    end
end

function endFunction()
    if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
        local part = game.Workspace.Monet.VaultEnter:FindFirstChild(playerName)
        part:Destroy()
    end
    
    disTanceText2 = 0
    local Player = game.Players:FindFirstChild(playerName)
    Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + Player.robInfo.cashTaken.Value
    Player.robInfo.cashTaken.Value = 0
    Player.PlayerGui.BankGui.Main.Visible = false
    playerName = ""
    
    --script.Parent.Parent.Bills.CoolDown.CoolDownStart:FireClient(Player)
end

script.Parent.Touched:Connect(function(hit)
    if script.Parent.Parent.Bills.Bank.Value == true then
            
    return end
            script.Parent.Parent.Bills.Bank.Value = true
    onTouch(hit)
end)
script.Parent.Parent.DP.Touched:Connect(robberCheck)

Once again I hope one of you out there can decipher this and help us out!

Could you put the script into Lua? Like this:

--Test

(That way I can read the whole script and hopefully find your issue)
(Providing me with the output would help as well)

local disTanceText2 = 0

local function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if script.Parent:FindFirstChild(hit.Parent.Name) then
			
		return end
		if script.Parent.Parent.Bills.Bank.Value == true then
			
		return end
		local Player = game.Players:FindFirstChild(hit.Parent.Name)
		playerName = hit.Parent.Name
		local part = Instance.new("Part")
		part.Anchored = true
		part.Parent = script.Parent
		part.Name = hit.Parent.Name
		part.Position = script.Parent.Position
		part.CanCollide = false
		part.Transparency = 1
		script.Parent.Parent.Bills.Bank.Value = true
	end
	showGui()
end

function showGui()
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
			if player.Team == game:GetService("Teams").Police then
			local clone = game.ReplicatedStorage.BankRob:Clone()
			clone.Parent = player.PlayerGui
		end
	end
	distanceCheck()
end

function distanceCheck()
	repeat 
		local Player = game.Players:FindFirstChild(playerName)
		if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
			local disTance = Player:DistanceFromCharacter(Vector3.new(game.Workspace.Monet.VaultEnter:FindFirstChild(playerName).Position))
			local disTanceText1 = math.floor(disTance)	
			disTanceText2 = disTanceText1
		end
			wait(0.1)
			print(disTanceText2)
	until disTanceText2 > 550
	endFunction()
end

function endFunction()
	if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
		local part = game.Workspace.Monet.VaultEnter:FindFirstChild(playerName)
		part:Destroy()
	end
	disTanceText2 = 0
	local Player = game.Players:FindFirstChild(playerName)
	Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + Player.robInfo.cashTaken.Value
	Player.robInfo.cashTaken.Value = 0
	Player.PlayerGui.BankGui.Main.Visible = false
	script.Parent.Parent.Bills.CoolDown.CoolDownStart:FireClient(Player)
	script.Parent.Parent.Bills.Bank.Value = false
	script.Parent.Script.Disabled = true
end

script.Parent.Touched:Connect(onTouch)

Like this? sorry if I did it wrong lol

Wasn’t what I had in mind, but it works. xD

1 Like

For anyone who wants the script in Lua, this is:

local playerName = “”
local disTanceText2 = 0

local function onTouch(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
if script.Parent:FindFirstChild(hit.Parent.Name) then

  return end
  if script.Parent.Parent.Bills.Bank.Value == true then
  	
  return end
  local Player = game.Players:FindFirstChild(hit.Parent.Name)
  playerName = hit.Parent.Name
  local part = Instance.new("Part")
  part.Anchored = true
  part.Parent = script.Parent
  part.Name = hit.Parent.Name
  part.Position = script.Parent.Position
  part.CanCollide = false
  part.Transparency = 1
  script.Parent.Parent.Bills.Bank.Value = true
end
showGui()
end

function showGui()
for _, player in pairs(game:GetService(“Players”):GetPlayers()) do
if player.Team == game:GetService(“Teams”).Police then
local clone = game.ReplicatedStorage.BankRob:Clone()
clone.Parent = player.PlayerGui
end
end
distanceCheck()
end

function distanceCheck()
repeat
local Player = game.Players:FindFirstChild(playerName)
if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
local disTance = Player:DistanceFromCharacter(Vector3.new(game.Workspace.Monet.VaultEnter:FindFirstChild(playerName).Position))
local disTanceText1 = math.floor(disTance)
disTanceText2 = disTanceText1
end
wait(0.1)
print(disTanceText2)
until disTanceText2 > 550
endFunction()
end

function endFunction()
if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
local part = game.Workspace.Monet.VaultEnter:FindFirstChild(playerName)
part:Destroy()
end
disTanceText2 = 0
local Player = game.Players:FindFirstChild(playerName)
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + Player.robInfo.cashTaken.Value
Player.robInfo.cashTaken.Value = 0
Player.PlayerGui.BankGui.Main.Visible = false
script.Parent.Parent.Bills.CoolDown.CoolDownStart:FireClient(Player)
script.Parent.Parent.Bills.Bank.Value = false
script.Parent.Script.Disabled = true
end

script.Parent.Touched:Connect(onTouch)

I am not 100% sure this is your issue, but in the function ‘distanceCheck’ the variable ‘playerName’ seems to have no assigned value, or I’m not observant enough. Is this your issue? Okay, so I found where you assigned a value to that variable, but that method is not what you want, as accessories such as hats can interfere (Note, your friend should indent their scripts. It makes it easier to edit and read.)

You will need to use a Remote Event to make sure you are sending the correct value to that variable. Simply insert a LocalScript into StarterPlayerScripts and have it fire an event with the LocalPlayer’s name. That way you know the serverscript inside the door is collecting the correct information, and not something else located inside the players character.

Please tell me if that isn’t working, and I’ll look into it more. :+1:

My friend would like to ask should it be the players name as the correct information?

Yes, if you want the ‘playerName’ variable to be the local players name, you must use a Remote Event to send it over to the serverscript. Keep in mind that the local player’s name must be given to the Serverscript when the time comes to use it. (This means the LocalScript must give the local player’s name once the part is touched, while the serverscript waits for a response.) So sorry if you’re confused!

we’re currently adding the check but it already is the players name that is currently robbing

Okay, I’ll look into it more. You’ll still want to use that method though, as it allows a 100% guarantee that it’s the players name.

I’ve looked through the script many times and couldn’t find a thing! (Heads up: You and your friend may want to practice tidying up scripts, so they are easier for others to read.) So sorry I couldn’t help with your main issue, but you are probably better off with someone more experienced. Again, sorry I couldn’t help much!

The remote event check might fix our problem

Okay, notify me once you get your results. I’m tired at the moment, (It’s 3:30 AM) and I can’t think very well.
:sweat_smile:

1 Like

Hey! We have kind of run into a new problem here. I updated the post a little bit with how we have updated the code and hopefully you can decipher it. We changed it so instead of walking 550 studs away you must instead drop off the cash to be awarded it in the leader stats. The new problem we are having now is that once you walk up to the brick that awards you the cash at the drop off, it is not awarding you the cash nor getting rid of the GUI that says how much cash you have. Here is the updated code:

local disTanceText2 = 0

local function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        
        if script.Parent:FindFirstChild(hit.Parent.Name) then
            
        return end
        
        local Player = game.Players:FindFirstChild(hit.Parent.Name)
        
        game.ReplicatedStorage.NameCheck:FireClient(Player)
        
        game.ReplicatedStorage.NameCheck.OnServerEvent:Connect(function(Player, Name)
            playerName = Name
            print(playerName .. Name)
        end)
        
        playerName = hit.Parent.Name
        local part = Instance.new("Part")
        part.Parent = script.Parent
        print(playerName)
        part.Name = hit.Parent.Name
        part.Position = script.Parent.Position
        part.Transparency = 1
        script.Parent.Parent.Bills.Bank.Value = true
    end
    
    showGui()
end

function showGui()
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
            if player.Team == game:GetService("Teams").Police then
            local clone = game.ReplicatedStorage.BankRob:Clone()
            clone.Parent = player.PlayerGui
        end
    end
end

function robberCheck(hit)
    if hit.Parent.Name == playerName then
        endFunction()        
    end
end

function endFunction()
    if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
        local part = game.Workspace.Monet.VaultEnter:FindFirstChild(playerName)
        part:Destroy()
    end
    
    disTanceText2 = 0
    local Player = game.Players:FindFirstChild(playerName)
    Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + Player.robInfo.cashTaken.Value
    Player.robInfo.cashTaken.Value = 0
    Player.PlayerGui.BankGui.Main.Visible = false
    playerName = ""
    
    --script.Parent.Parent.Bills.CoolDown.CoolDownStart:FireClient(Player)
end

script.Parent.Touched:Connect(function(hit)
    if script.Parent.Parent.Bills.Bank.Value == true then
            
    return end
            script.Parent.Parent.Bills.Bank.Value = true
    onTouch(hit)
end)
script.Parent.Parent.DP.Touched:Connect(robberCheck)

Could you show me which part is reporting an error? (if one at all) Also, sorry for the late reply. I’ve been offline for a while.