#When touched instead of just adding one it crashes the game entirely.

1 messages · Page 1 of 1 (latest)

gentle owl
#

I'm trying to make this code so when the Pickaxe touches the Wall it give one gem (or point to the value) and then it ends. But when I test it it crashes studio entirely....

Code:

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    
    
    
    local gemstats = Instance.new("IntValue")
    gemstats.Name = "Gems"
    gemstats.Value = 1
    gemstats.Parent    = leaderstats
    
    
    while true do
        local Wall = game.Workspace.Part
        local diamondarmor = game.StarterPack:FindFirstChild("Pickaxe")
        Wall.Touched:Connect(function(otherPart)
            
            if diamondarmor then
                
                gemstats.Value = gemstats.Value + 1
                
                
                
            end
            
            
        end)
    end
end)```
rich haven
#

wait()

#

task.wait()

#

Also .Touched already do a loop.

#

So remove your while true do loop, that's it

gentle owl
#

imma try it

gentle owl
rich haven
#

Wdym?

gentle owl
#

when i use the pickaxe it goes to 4

rancid willowBOT
#

studio** You are now Level 1! **studio

rich haven
#

Ye, you're using .Touched

#

Learn RayCast or Region3

velvet perch
rich haven
#

:GetPartInBounds isn't Region3?

#

Cuz that's what I meant

gentle owl
#

?

rich haven
#

You can still use it by checking if it has been hit already

gentle owl
rich haven
#
local TouchCooldown = 1

local Touched = false
Part.Touched:Connect(function()
    if not Touched then
        Touched = true
        delay(TouchCooldown, function()
            Touched = false
        end)
    end
end)
velvet perch
rich haven
#

I don't get why you're talking but that is right

#

You can do this:

local TouchCooldown = 1

local Touched = false
Part.Touched:Connect(function()
    if not Touched then
        Touched = true
        wait(TouchCooldown)
        Touched = false
    end
end)
#

Add your

if diamondarmor then
    gemstats.Value = gemstats.Value + 1
end

after the wait.

gentle owl
#

does this make it so it touches it once?

#

like debounce?

rich haven
#

No, it's a cooldown, you can make it small

gentle owl
#

oh

rich haven
#

Anyways, for a Pickaxe, you should use another method

gentle owl
#

like what

rich haven
#
local Pickaxe = script.Parent
local Handle = Pickaxe:WaitForChild("Handle")

local Player = Pickaxe:FindFirstAncestorOfClass("Player")

Pickaxe.Activated:Connect(function()
    local Parts = workspace:GetPartBoundsInBox(Handle.CFrame, Vector3.new(4, 4, 4))
    for _, Part in Parts do
        if Part:HasTag("Ore") then
            Player.leaderstats.gemstats.Value += 1
        end
    end
end)
#

This is a Script inside the Tool

gentle owl
#

you made this or its in the pickaxe already

rich haven
#

The script? I made it

gentle owl
#

oh ok

rancid willowBOT
#

studio** You are now Level 2! **studio

gentle owl
#

oh i get it

rich haven
gentle owl
#

thanks very helpful

#

do you know how to make the block (in this case wall) teleport after

#

i made it so that it could be destroyed but i dont know how to make it teleport somewhere else near

rich haven
#

You trying to make a mining game?

gentle owl
#

i guess

#

i just wanna test something out

rich haven
#

Mining games randomly generates the terrain with all the ores

gentle owl
rich haven
#

If you want to Teleport a part, you have to change it's CFrame/Position property

rich haven
#

You have to do it on server for all clients to see the change

gentle owl
#

so uhh

#

serverscriptservice

#

also when I dont have the tool equipped it still destroys a wall...

#

the wall

rich haven
#

With the old .Touched or the .Activated thing I made?

gentle owl
#

lemme put that in

#

do i delete the script i made or

rich haven
#

You just remove the .Touched part of your script

#

Not the Leaderstats setup

gentle owl
#

oh

#

my script wasnt even in the pickaxe

rich haven
#

Your script that sets leaderstats needs to be in ServerScriptService

#

Only the hitbox thing is in the Tool

gentle owl
#

ok

#
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player




    local gemstats = Instance.new("IntValue")
    gemstats.Name = "Gems"
    gemstats.Value = 1
    gemstats.Parent  = leaderstats```
For my leaderstats thing it says that expectd end to close function at line 1 got <eof>
rich haven
#

Ye

#

end)

gentle owl
#

oh yeah

#

the pickaxe code doesnt work

rich haven
#

:/

#

You copied it properly?

gentle owl
#

yeah

rich haven
#

You have a Handle in your tool?

gentle owl
#

yeah

rich haven
#

Weird

#

This perfectly works for me:

#

Also the VisualPart was just for the recording so you could see the Hitbox

#

Um

#

I know what could be the issue

#

Your part needs to have the "Ore" tag

gentle owl
#

how do i get that..

rich haven
#

Look at the end of the video I sent earlier

#

I show it briefly

gentle owl
#

k

rancid willowBOT
#

studio** You are now Level 3! **studio

gentle owl
#

it says that gemstats is not a valid member of Folder Players.MyName.leaderstats

rich haven
#

Ah yes it's Gems

#

I wasn't paying attention

gentle owl
#

?

rich haven
#

Replace gemstats with Gems

gentle owl
#

oh ok

#

Hey it works thanks!!

rich haven
#

Np

gentle owl
#

now to do the teleportation of the walls

rich haven
#

What do you want to do?

#

Teleport wall when Pickaxe touchs it?

gentle owl
#

yet

#

yeah

rich haven
#

Where should it teleports to?

gentle owl
#

20 blocks away from me

#

somewhere close

#

it has to do something with cframe right?

rich haven
#

Yes

gentle owl
#

math.random?

rich haven
#
local Range = {16, 20}
local Pickaxe = script.Parent
local Handle = Pickaxe:WaitForChild("Handle")

local Player = Pickaxe:FindFirstAncestorOfClass("Player")

Pickaxe.Activated:Connect(function()
    local Parts = workspace:GetPartBoundsInBox(Handle.CFrame, Vector3.new(4, 4, 4))
    for _, Part in Parts do
        if Part:HasTag("Ore") then
            Player.leaderstats.Gems.Value += 1
            local CenterPos = Handle.CFrame.Position
            Part.CFrame = CFrame.new(Vector3.new(
                    CenterPos.X + (math.random(Range[1], Range[2]) * (math.random(1, 2) == 1 and -1 or 1)),
                    CenterPos.Y + (math.random(Range[1], Range[2]) * (math.random(1, 2) == 1 and -1 or 1)),
                    CenterPos.Z + (math.random(Range[1], Range[2]) * (math.random(1, 2) == 1 and -1 or 1))
            ))
        end
    end
end)
#

The CFrame code is big, you probably can make it better, I just made something that should work

gentle owl
#

i put this in the wall ?

rich haven
#

No it is the Tool script

gentle owl
rich haven
#

Part.CFrame

#

It changes the CFrame of the Part the hitbox touched

gentle owl
#

i dont see the wall anymore 😦

rich haven
#

It's maybe under the floor

gentle owl
#

it is lol

rich haven
#

Change the Y value

gentle owl
#

to 0?

rich haven
#

To the good height

Part.CFrame = CFrame.new(Vector3.new(
    CenterPos.X + (math.random(Range[1], Range[2]) * (math.random(1, 2) == 1 and -1 or 1)),
    4,
    CenterPos.Z + (math.random(Range[1], Range[2]) * (math.random(1, 2) == 1 and -1 or 1))
))
#

Depends on your map

gentle owl
#

its just the baseplate for now