When I mine a rock in my game (Click it 10 times) it mines and gives 100 coins. However if someone else does it it gives it to me? Can anyone h3lp? - roblox

So when I click it 10 times it mines however it only give coins to me when someone else mines it. I have no clue really what to do so i need help. Ok so here is the script:
game.Players.PlayerAdded:Connect(function(player)
script.Parent:Clone()
local money = player:WaitForChild("leaderstats").Money
script.Parent.ClickDetector.MouseClick:Connect(function()
local billboard = script.Parent.BillboardGui
print(health)
health = health - 1
billboard.Enabled = true
billboard.TextLabel.Text = health
if health == 0 then
local clickdetector = script.Parent.ClickDetector
billboard.Enabled = false
money.Value = money.Value + 100
script.Parent.ClickDetector.Parent = nil
script.Parent.Union.Transparency = 1
script.Parent.Union.CanCollide = false
wait(5)
clickdetector.Parent = script.Parent
script.Parent.Union.Transparency = 0
script.Parent.Union.CanCollide = true
health = 10
end
end)
end)

The problem is that you need to make sure the player variable matches the player that clicked it.
script.Parent.ClickDetector.MouseClick:Connect(function()
should be:
script.Parent.ClickDetector.MouseClick:Connect(function(clicker)
and use an if statement:
if clicker == player then
--code that gives points
end
Also when you clone the parent it clones the script so that may cause bugs.

Related

Roblox Functions are running 20+ times

I seem to be having an issue of every time I want to pass a function when an event happens such as a Humanoid.Died() or a BindableEvent it runs the function 20+ times, also adding to an IntegerValue that many times as well. I have tried debounces, BindableEvents, connection:Disconnect(), etc. Is there something I'm doing wrong or missing?
Updated original code:
function module.damageHumanoid(attacker, player, humanoid, damage)
local connection
local debounce = false
connection = humanoid.Died:Connect(function()
if not debounce then
debounce = true
connection:Disconnect()
module.onDeath(player)
end
end)
if player:FindFirstChild("damage") == nil then
newFolder = Instance.new("Folder")
newFolder.Name = "damage"
newFolder.Parent = player
end
if newFolder:FindFirstChild(tostring(attacker)) == nil then
record = Instance.new("IntValue")
record.Name = tostring(attacker)
record.Parent = newFolder
end
if record.Value >= 100 then -- sanity check
else
record.Value = record.Value + damage
end
return humanoid:TakeDamage(damage)
end
Which triggers this code:
function module.onDeath(player)
if player:FindFirstChild("damage") ~= nil then
local folder = player:WaitForChild("damage")
local contents = folder:GetChildren()
for i,v in pairs(contents) do
local item = folder[tostring(v)]
damages[item.Name] = item.Value
end
end
local debounce = false
if not debounce then
debounce = true
module.handleKill(damages)
end
end
This is the sword script's function:
function Blow(Hit)
if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
return
end
local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
if not RightArm then
return
end
local RightGrip = RightArm:FindFirstChild("RightGrip")
if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
return
end
local character = Hit.Parent
if character == Character then
return
end
local humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid or humanoid.Health == 0 then
return
end
local player = Players:GetPlayerFromCharacter(character)
if player and player == Player then
return
end
UntagHumanoid(humanoid)
TagHumanoid(humanoid, Player)
module.damageHumanoid(plur, player, humanoid, Damage)
end
So I guess the way I was trying to use debounce isn't correct anymore. I referred to the Roblox documentation and found this: https://create.roblox.com/docs/scripting/scripts/debounce
If anyone would like to know what ended up working for me here it is:
humanoid.Died:Connect(function()
if not humanoid:GetAttribute("Killed") then
humanoid:SetAttribute("Killed", true)
module.onDeath(player)
task.wait(10)
humanoid:SetAttribute("Killed", true)
end
end)

Boat Spawn (Roblox Studio)

I'm trying(I'm very new) to make a game where you have to attack each other with a boat. There is a button on each island and when you press it, a boat should appear. (this works so far)
But as soon as you want to spawn a new boat it removes everyones boat. I thought it makes sense to use player.id but this still won't work. Does anyone know the solution?
Below you will find the code thats hidden in a part.
Thanks!
CODE:
local cd = script.Parent.ClickDetector
local boat = script.Parent.Parent.Firstboat
local button = script.Parent
local debounce = false
game.Players.PlayerAdded:Connect(function(player)
print(player.UserId)
local plruserid = player.UserId
print(player.Name)
boat.Parent = game.ServerStorage
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function()
local SetNameToBoat = plruserid
print (SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
if not debounce then
if oldboat then
oldboat : destroy()
end
debounce = true
local NewBoat = boat:clone()
NewBoat.Name = (SetNameToBoat)
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)
end)
I simplified the code and found the solution:
local cd = workspace.Button:WaitForChild('ClickDetector')
local boat = game:GetService('ServerStorage'):WaitForChild('Boat')
local button = workspace:WaitForChild('Button')
local debounce = false
cd.MouseHoverEnter:Connect(function()
button.Transparency = 0.5
end)
cd.MouseHoverLeave:Connect(function()
button.Transparency = 0
end)
cd.MouseClick:Connect(function(player)
local plruserid = player.UserId
local SetNameToBoat = plruserid
print(SetNameToBoat)
local oldboat = workspace:FindFirstChild(SetNameToBoat)
if not debounce then
if oldboat then
oldboat:destroy()
end
debounce = true
local NewBoat = boat:clone()
NewBoat.Name = SetNameToBoat
NewBoat.Parent = game.Workspace
wait(5)
debounce = false
end
end)

ROBLOX - How would I move the player 8 studs, for 20 miliseconds smoothly (so tweening), in the direction the player is facing?

How would I move the player 8 studs, for 20 miliseconds smoothly (so tweening), in the direction the player is facing?
script.Parent.Activated:Connect(function()
--[This is because this is a localscript in a tool]
end)
local isGliding = false;
script.Parent.Activated:Connect(function()
isGliding = not isGliding;
local char = script.Parent.Parent; --The tool gets inside of the char when activated
local humanoidRootPart = char:WaitForChild("HumanoidRootPart");
coroutine.wrap(function()
while true do
humanoidRootPart.Positon += humanoidRootPart.LookVector * 8;
wait(0.3)
if isGliding == false then
break;
end
end
end)()
end)
Something like that probably. This should be in a ServerScript under the tool.

How do I change a value in the leaderstats via touching a part? (Roblox)

Here's the error and what I've tried.
Error: Workspace.Part.Script:4: attempt to index nil with 'leaderstats'
My code:
Making Leaderstats:
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Model")
ls.Name = "leaderstats"
ls.Parent = plr
local m = Instance.new("IntValue")
m.Name = "Stars"
m.Value = 0
m.Parent = ls
end)
Touch Code:
local collected = false
script.Parent.Touched:Connect(function()
if collected == false then
game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
end
collected = true
end)
A server script would suit your need better. In a localscript, the change would only appear for the player. Also, it's better practice to use the Players service offered by Roblox. Here's an example:
local Players = game:GetService('Players')
local collected = false
script.Parent.Touched:Connect(function(partTouched)
if partTouched.Parent:IsA('Model') and partTouched.Parent:FindFirstChild('Humanoid') and partTouched:IsA('BasePart') then -- Is this a player? If so, then..
local player = Players:GetPlayerFromCharacter(partTouched.Parent)
if collected == false then
player.leaderstats.Stars.Value += 1 -- shorter syntax
end
collected = true
--script.Parent:Destroy() optional if this part won't be used again.
end
end)
If you're planning to use this for many parts, using a central ModuleScript would save you lots of changing things back and forth: https://developer.roblox.com/en-us/api-reference/class/ModuleScript

How to animate (moving by default) and inanimate (make stationary on tap) colliding objects

Hey tired this new code to animate objects (here bubbles) and make them stationary using sprites. The code is as below,
local ui = require("ui")
local gameUI = require("gameUI")
local easingx = require("easingx")
require "sprite"
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
physics.setScale( 60 )
local backgroundPortrait = display.newImage( "sky.png", 0, 0 )
local backgroundLandscape = display.newImage( "sky.png", 80, 80 )
backgroundLandscape.isVisible = false
local disp = backgroundLandscape
local function selectBubble( event )
local tapped = event.target --event.target is how Corona points to the tapped bubble
if ( tapped.bubbleSelected == false ) then
local vx,vy = tapped:getLinearVelocity()
tapped.xVel = vx --stores the current velocity into bubble's "xVel" variable
tapped.yVel = vy --likewise for yVel
tapped:setLinearVelocity( 0,0 ) --set bubble's velocity to 0!
tapped.currentFrame = (3)
tapped.bubbleSelected = true
elseif ( tapped.bubbleSelected == true ) then
tapped:setLinearVelocity( tapped.xVel, tapped.yVel ) --read previous velocity and set
tapped.bubbleSelected = false
end
end
--BUBBLE1
local bubble1 = sprite.newSprite( spriteSet1 )
bubble1.x = 100
bubble1.y = 100
physics.addBody(bubble1, {bounce=0.04, filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 2, 4 )
bubble1:addEventListener( "tap", selectBubble )
bubble1.bubbleSelected = false
bubble1:prepare("bubble")
bubble1:play()
--BUBBLE2
local bubble2 = sprite.newSprite( spriteSet1 )
bubble2.x = 210
bubble2.y = 20
physics.addBody(bubble2, {bounce=0.05, filter = bubbleCollisionFilter})
bubble2:setLinearVelocity( 2, 4 )
bubble2:prepare("bubble")
bubble2:play()
--BUBBLE3
local bubble3 = sprite.newSprite( spriteSet1 )
bubble3.x = 100
bubble3.y = 17
physics.addBody(bubble3, {bounce=0.02, filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 1, 2 )
bubble3:prepare("bubble")
bubble3:play()
--BUBBLE4
local bubble4 = sprite.newSprite( spriteSet1 )
bubble4.x = 310
bubble4.y = 20
physics.addBody(bubble4, {bounce=0.4, filter = bubbleCollisionFilter})
bubble4:setLinearVelocity( 2, 4 )
bubble4:prepare("bubble")
bubble4:play()
The problem is firstly the code doesn't seem to work. Secondly though the bubble changes color on tap (this color is same for most). Yet, each bubble has a unique letter on it. How to get this to work. Please help.
It's hard to know what exactly you're asking because your question is pretty vague (what does "animate" mean? And what do you mean by "doesn't seem to work"? explain these terms because they can mean multiple things) but I think you want to set the physics bodies to "static" in the tap event listener. Refer here for what I'm talking about:
http://developer.anscamobile.com/reference/index/bodybodytype
So inside the selectBubble() function you would type something like tapped.bodyType="static"