Hello again developers, I hope you are having an amazing day! Me and a friend are trying to create a tycoon to learn more about scripting and game creation and have come upon an issue in our script. We are trying to make an entrance to claim a tycoon. For some reason when a player walks in with a gear such as a sword, the entrance does not work for them and even if they put away the sword, the entrance still does not work. The other entrances work just fine for the player if they do not have a sword in hand. If they do though, the same issue persists with the other doors as well. We have no idea how to fix this, what should we do?
It doesn’t give any errors in the output so we do not know what is going wrong.
debounce = false
script.Parent.Head.Touched:Connect(function(hit)
if debounce == false then
debounce = true
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
wait()
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.Team = game.Teams["Purple Tycoon"]
script.Parent.Name = player.Name .. "'s Tycoon"
script.Parent.Head.Transparency = 0.9
local ownstycoon = Instance.new("BoolValue", game.ServerStorage)
ownstycoon.Name = "Ownstycoon"
ownstycoon.Value = true
local begin = workspace.Folder["Begin Working"]:Clone()
begin.Parent = workspace.Folder
begin:SetPrimaryPartCFrame(CFrame.new(Vector3.new(-116.928, 499.337, -533.66)))
begin.Head.Rotation = Vector3.new(0, 0, -90)
end
end
end)
Rule #1 of debugging is read the error messages. They will go a long way toward helping you figure out what went wrong and where.
That being said, you didn’t give us what the error message was so I can only guess what went wrong.
My best guess is here in this part of the code:
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player = player.leaderstats.Cash.Value
print(player)
if player ~= nil then
If a gear is what hits the entrance, then hit.Parent is probably going to be the Tool of the gear, not the player holding it. You should put player = player.leaderstats.Cash.Value inside the if-then block because that line of code assumes player is not nil.
Also, what exactly is the point of player = player.leaderstats.Cash.Value? I would assume Cash is a Number- or IntValue; why are you trying to assign player to its value?
It doesn’t give any errors in the output so we do not know what is going wrong.
Alright I guess I’m just really blind today, yikes. Apologies.
Also, you should probably set debounce to false when the code finishes so nothing gets stuck, which might be why the tycoon stays unclaimable when a tool hits the entrance before the player’s body does.
Alright, edited the debounce and now you can go through it only if you put your sword back into your inventory, how do i make it so that you can walk through with the sword in your hand?
Now when you go through it it just says tool until you take your sword out. I want the player to be able to walk through the entrance with a sword or tool and it still works
debounce = false
script.Parent.Head.Touched:Connect(function(hit)
if debounce == false then
if hit:IsA("Tool") then
debounce = true
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
wait()
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.Team = game.Teams["Purple Tycoon"]
script.Parent.Name = player.Name .. "'s Tycoon"
script.Parent.Head.Transparency = 0.9
local ownstycoon = Instance.new("BoolValue", game.ServerStorage)
ownstycoon.Name = "Ownstycoon"
ownstycoon.Value = true
local begin = workspace.Folder["Begin Working"]:Clone()
begin.Parent = workspace.Folder
begin:SetPrimaryPartCFrame(CFrame.new(Vector3.new(-116.928, 499.337, -533.66)))
begin.Head.Rotation = Vector3.new(0, 0, -90)
end
else
debounce = false
print("tool")
end
end
end)
See the edit he made; it should be hit.Parent:IsA("Tool").
Also, you should be able to just do this and it’ll work fine:
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
debounce = true
-- Continue assigning the tycoon and stuff
end
end
This way, debounce is only set to true when we found a value for player without the need to check for whether the nth parent of hit is a player’s character. You can’t always assume that hit.Parent.Parent will be the player’s character model, after all.
Edit 2: So using this method, your script would look like:
debounce = false
script.Parent.Head.Touched:Connect(function(hit)
if not debounce then -- same thing as if debounce == false then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then -- same thing as if player ~= nil then
debounce = true
player.Team = game.Teams["Purple Tycoon"]
script.Parent.Name = player.Name .. "'s Tycoon"
script.Parent.Head.Transparency = 0.9
local ownstycoon = Instance.new("BoolValue", game.ServerStorage)
ownstycoon.Name = "Ownstycoon"
ownstycoon.Value = true
local begin = workspace.Folder["Begin Working"]:Clone()
begin.Parent = workspace.Folder
begin:SetPrimaryPartCFrame(CFrame.new(Vector3.new(-116.928, 499.337, -533.66)))
begin.Head.Rotation = Vector3.new(0, 0, -90)
debounce = false -- To be safe; remove this if you set debounce to false at some other point already
end
end
end)