Im making a portal, you must have 100 coins to go through it, no errors pop up
script.Parent.Touched:Connect(function(hit)
if hit.Name == "HumanoidRootPart" then
if game:GetService("Players")[hit.Parent.Name].leaderstats.Coins.Value >= 100 then
game:GetService("ReplicatedStorage").checkData:FireClient(game:GetService("Players")[hit.Parent.Name], "Portal1")
end
end
end)
starter player scripts
game:GetService("ReplicatedStorage").checkData.OnClientEvent:Connect(function(portal)
if portal == "Portal1" then
if game:GetService("Workspace").Portal1.Main:FindFirstChild("Info") then
if game:GetService("Workspace").Portal1.Main.CanCollide == true then
game:GetService("Workspace").Portal1.Main.CanCollide = false
game:GetService("Workspace").Portal1.Main.Info:Destroy()
end
end
end
end)
Try instead of checking in your first script if hit.Name == “HumanoidRootPart”, check if :GetPlayerFromCharacter(Hit.Parent) is not false.
Also, side note, good job using :GetService() for thing like Players as opposed to the . syntax. Lots of people don’t bother to but it’s good practice regardless.
You can also simplify game:GetService(“Workspace”) to just one word, workspace, lowercase.
script.Parent.Touched:Connect(function(hit)
if hit.Name == "HumanoidRootPart" then
if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) == false then
game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) = true
end
if game:GetService("Players")[hit.Parent.Name].leaderstats.Coins.Value >= 100 then
game:GetService("ReplicatedStorage").checkData:FireClient(game:GetService("Players")[hit.Parent.Name], "Portal1")
end
end
end)
Close, you just need to check it once at the top since the function returns true or false based on whether or not the instance is a Player’s character>
script.Parent.Touched:Connect(function(hit)
if game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then
if game:GetService("Players")[hit.Parent.Name].leaderstats.Coins.Value >= 100 then
game:GetService("ReplicatedStorage").checkData:FireClient(game:GetService("Players")[hit.Parent.Name], "Portal1")
end
end
end)
Usually for touched events I do this: if hit.Parent:FindFirstChild('HumanoidRootPart') ~= nil then
You want to check if HRP is present. You basically checked if the player’s name is HumanoidRootPart
Edit:
if hit.Parent:FindFirstChild('HumanoidRootPart') ~= nil then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- variable for the player
if plr.leaderstats:WaitForChild('Coins').Value >= 100 then
-- code
end
end
function doorTouched(hit)
if hit.Parent:FindFirstChild('HumanoidRootPart') ~= nil then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- variable for the player
if plr.leaderstats:WaitForChild('Coins').Value >= 100 then
local event = game.ReplicatedStorage:WaitForChild('checkData')
event:FireClient(plr,'Portal1')
print('event fired') -- just to check if the code ran correctly
end
end
end
script.Parent.Touched:Connect(doorTouched)