Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my script not work? Can a variable not have more than one of the same beginning?

What I mean is, I have a script in a part. When I click the part, A noise is supposed to play, and then a ball is to be created at the given vector. It also checks if an "IntValue" is atleast 5 and then subtracts 5. But nothing works, and no error is in the output. Here's the script:

local part = script.Parent
local db = false

function gum(hit)
	local human = hit.Parent
	local character = game.Players:GetPlayerFromCharacter(hit.Parent)
	if character then
		if not db then
			
			db = true
			
			if character.leaderstats.Money.Value >= 5 then
				
				character.leaderstats.Money.Value = character.leaderstats.Money.Value - 5
				
			
			
			part.Sound:Play()
			wait(2)
			local ball = game.Lighting.ball
			ball:Clone().Parent = game.Workspace
			ball.Position = Vector3.new(31,12,-6)
			ball.Anchored = false
			
			db = false
			
			else
				print("You need more money!")
				
			end
		end
	end
end

part.ClickDetector.MouseClick:connect(gum)

All help is appreciated!

2 answers

✓ Accepted answer

Firstly, playing a sound in a part won't play the sound. You either play it globally via workspace, or locally via PlayerGui. Parenting the sound to the workspace before playing it should fix the issue with sound.

As for the ball, you're telling the script to position a part in Lighting, so of course, you're not going to see it. I'm assuming that you're trying to position the clone, because if so, you're positioning the one in Lighting.

Thirdly, ClickDetectors automatically grab the player, so you don't need the player bit.

Your code, but with minor fixes:

local part = script.Parent
local db = false

function gum(hit)
    if hit then
        if not db then

            db = true

            if hit.leaderstats.Money.Value >= 5 then

                hit.leaderstats.Money.Value = hit.leaderstats.Money.Value - 5



            local sound = part.Sound
            sound.Parent = workspace
	    sound:Play()
            wait(2)
            local ball = game.Lighting.ball:Clone()
            ball.Parent = game.Workspace
            ball.Position = Vector3.new(31,12,-6)
            ball.Anchored = false

            db = false

            else
                print("You need more money!")

            end
        end
    end
end

part.ClickDetector.MouseClick:connect(gum)


If this helped you, please don't forget to upvote and accept the answer.

2 comments

1

Ok, this worked, but one last question. So, I don't need to define character in Click events? The parameter does it for me? But in Touch events I do, am I correct?

1

In Clicked events, there is no specific parts clicking the part. Just player interaction. With touched events, there is a part hitting another part. So you would have to go through the character through that.

##Problem

ClickDetectors automatically get the player object. Since it's not a particular part hitting the ClickDetectors but a player interaction, it would make sense for the parameter to be a player object. If you were to use the .Touched event of parts, then this method would work.

##Solution

Forget about trying to get through a character and just use the player parameter.

You would also want to put db = false at the end of the else code for the if then statement on line 10. Otherwise the button would be stuck.

##Final Script

local part = script.Parent
local db = false

function gum(player) --Parameter given to the function is the player through the click detector, not a part of the character.
    if player then
        if not db then

            db = true

            if player.leaderstats.Money.Value >= 5 then
                player.leaderstats.Money.Value = player.leaderstats.Money.Value - 5

                part.Sound:Play()
                wait(2)
                local ball = game.Lighting.ball
                ball:Clone().Parent = game.Workspace
                ball.Position = Vector3.new(31,12,-6)
                ball.Anchored = false

                db = false

            else
                print("You need more money!")
                db = false --You do not want your button getting stuck.
            end

        end
    end
end

part.ClickDetector.MouseClick:connect(gum)

######Hopefully, this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment below.

3 comments

0

Don't I need to define character? Or I don't need to..?

0

Leaderstats are in the player or character? Think hard about this... Updating answer.

0

Wait, I see what you did. You just turned character to player. Thanks, upvoted.

This discussion is preserved as it was. New answers, comments, and votes are closed.