I want to make a coin that will give you coins when you step on it. Once you step on it it teleports away.
I made this script
local amnt = 1
local canCollectCoin = true
local isTouchingCoin = false
local X1 = script.Parent.Parent.X1.Position.X
local X2 = script.Parent.Parent.X2.Position.X
local Z1 = script.Parent.Parent.Z1.Position.Z
local Z2 = script.Parent.Parent.Z2.Position.Z
if X1 > X2 then
X1, X2 = X2, X1
end
if Z1 > Z2 then
Z1, Z2 = Z2, Z1
end
function onTouched(part)
local h = part.Parent:FindFirstChild("Humanoid")
if h and canCollectCoin and not isTouchingCoin then
isTouchingCoin = true
local thisplr = game.Players:FindFirstChild(h.Parent.Name)
if thisplr then
local stats = thisplr:FindFirstChild("leaderstats")
if stats then
local score = stats:FindFirstChild("Coins")
if score then
score.Value = score.Value + amnt
canCollectCoin = false
local randomX = math.random(X1, X2)
local randomZ = math.random(Z1, Z2)
script.Parent.CFrame = CFrame.new(Vector3.new(randomX, script.Parent.Position.Y, randomZ))
wait(5)
canCollectCoin = true
end
end
end
end
end
function onEnded(part)
if part.Name == "Humanoid" then
isTouchingCoin = false
end
end
script.Parent.Touched:Connect(onTouched)
script.Parent.TouchEnded:Connect(onEnded)
The script is suppose to give the player one coin and then teleport away. The only problem is this:
robloxapp-20230618-0938017.wmv (1.5 MB)
This is what the explorer looks like:
If you could help me with this it would be greatly appreciated