Roblox - while testing trusses work when i drag them in, but when i use script to set all the stuff, then i cant climb it - roblox

I am using a script to set cancollide = true and transparency = 0, but i cant climb the truss. but when I'm already in testing mode (in studio) and i drag in the same truss, i can climb it, and i am looking at properties, its same, both anchored is true, they are touching same parts, i dont know why this is happening. Please help thanks :)by the way, i am making tycoon and this is script i am using:
wait(1)
amount = 0 -- cost of model
owner = script.Parent.Parent.Owner
local stun = false
pcall(script.Parent.Head.Touched:connect(function(hit)
if hit.Parent ~= nil then
player = game.Players:findFirstChild(hit.Parent.Name)
if not stun and player ~= nil then
if player.Name == owner.Value then
if player:findFirstChild("leaderstats") ~= nil then
stats = player:findFirstChild("leaderstats")
if stats.Money.Value >= amount then
stun = true
stats.Money.Value = stats.Money.Value - amount
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
wait(1)
stun = false
end
end
end
end
end
end))
dont worry about other stuff, it works, its just this part that matters now:
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
Please help :( this is the problem with the ladder using script not working and same dragged in from toolbox truss working, iv done this with many trusses and ladders and same result :(

So, a error I noticed in your code, is that you erase your Character's Head, which would kill your character, or possibly throw a error if the .Touched event fires two times.
There are many efficency errors in this script, such as .Transparency = (0), or doing script.Parent.ladder instead of using variables, but it really is no problem most of the times. Something you could try, is to use Instance.new() to create the ladder.If this game is FilteringEnabled on (Expermiental mode off), please note your script won't work as intended if it is a LocalScript, it will only work well on Server-Side (aka, a normal Script instead of a LocalScript)

Related

Im making a PropHunt game in roblox but the whenever i morph the player character into a new form the UI keeps on being called

I have this project, im creating a prophunt game, however im in a little road block that i can't understand.
Here is how my code works, I have a local script which tells me which team the player is in, since the player does not have a team yet they are always assumed as neutral, then the local script is called whenever the players are added, which a GUI appears, allowing for them to pick the team.
Heres what i did for the Local script GUI, I'm just learning this for now so not really the best way i could have coded it right now, but it does simulate what i want to happen.
Player.PlayerAdded:Connect(function(player)
if player.Team == TeamService.Blue then
Frame.Visible = false
else
Frame.Visible = true
end
end)
Hiders.MouseButton1Click:Connect(function(player)
local result = ReplicatedStorage.RemoteFunction:InvokeServer(TeamHiders)
print(result)
if result == true then
Frame.Visible = false
else
Frame.Visible = false
end
end)
Here's what i did for the server script to assign the players a team, I'm sorry I'm still fairly new to roblox so i don't know if my approach is correct im just going with what closely gets me to the output.
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function (player, Team)
if Team == teamService.Blue then
player.Team = Team
print(ChoosenTeam)
return "player Team is blue"
end
if Team == teamService.Red then
player.Team = Team
print(player.Team)
return "player Team is red"
end
end
And here is my script for the Morph, so i got this click detector which should tell the game that whenever its clicked transform the player into whatever form this is scripted on.
local model = script.Parent.Parent
script.Parent.MouseClick:Connect(function(player)
if player.Team == teamservice.Blue then
local oldCharacter = player.Character
local newCharacter = model:Clone()
newCharacter.HumanoidRootPart.Anchored = false
newCharacter:SetPrimaryPartCFrame(oldCharacter.PrimaryPart.CFrame)
player.Character = newCharacter
newCharacter.Parent = workspace
end
end)
The players under blue side should only encounter this once since the UI should only appear when they do not have a team or are loaded in the game, but what happens is the blue players whenever they morph into a block they encounter the GUI prompt again.
I was expecting by limiting the player teams to nil it would only show the UI, and if the player has a team already then the UI would not show itself, the same goes to when the player decides to morph the player UI should not be visible.
So turns out i can just turn off spawn on reset.

Roblox - detect if mesh is hit by hand

I am not sure where to start so asking for some help. I want to create a script that detect if a certain mesh is hit by players right hand. Every time the mesh is hit I want to increase points for player by 1.
Anyone who can nudge me in the right direction here?
Thanks!
EDIT:
I have added this script in StarterCharacterScripts:
game.Players.LocalPlayer.Character:WaitForChild("RightHand").Touched:Connect(function(hit)
local part1 = workspace.CoinsClouds["Meshes/SackOfGoldNoCoins1"]
part1.Touched:Connect(function(hit)
if hit.Name == "RightHand" then
print(hit.Name)
end
end)
end)
This will register when I bump into the part with the right hand, BUT it will register it 5-20 times in a split second every time I bump into the part with the right hand. See attached image. Anyone know why? I would like it to register only once when the right hand is bumped against the part, or even better, only when the user punch the part/mesh. I have tried to add a wait after RightHand is found, but that doesn't work.
PS! I don't know if this is the right way to script it...
Below is an example of the code you would use. It will check if your part is touched by either the right or left hand and it will run whatever is inside of your if statement, in this case it will increase whatever your score is by 1.
local part = workspace.Part -- reference the part here
local debounce = false --used to check for debounce
part.Touched:Connect(function(hit)
if hit.Name == "RightHand" or "LeftHand" then -- checks if a hand touched it
if not debounce then --Check that debounce variable is not true
debounce = true --currently touching the part
print("hit")
wait(1)
debounce = false
end
end
end)
In relation to your new question it is to do with debounce. Roblox has an entire article on it here: https://developer.roblox.com/en-us/articles/Debounce
Basically you would have to add a new if statement to check that the player isn't already touching this part if it is it wont repeat the what is inside the if statement. The code sample above has been edited to work with debounce.

How do you teleport characters in Roblox without leaving residue?

How do you teleport characters in Roblox without causing the HumanoidRootPart to be out of sync with the character? I have found that moving just the HumanoidRootPart will cause the rest of the character to snap back in place where you want them to be, but to the server, it is still in place before the teleport. Is there a better method?
Yes, you can use model:MoveTo() to move models (characters are models).
For example, a simple teleporter:
local debounce = false
-- the teleporter is just a part with the script it's child
script.Parent.Touched:Connect(function (hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce then
debounce = true
local char = player.Character
local rootPart = char:WaitForChild("HumanoidRootPart")
char:MoveTo(rootPart.Position + Vector3.new(0, 10, 0))
wait(.5)
debounce = false
end
end)

Roblox - attempt to call a nil value

When trying to create a code, that on FireServer with Specific Player chosen a GUI would've been cloned into their PlayerGui, But instead I get a nil value.
Code:
local Blinder = game.ReplicatedStorage.RCD.Blind
Blinder.OnServerEvent:Connect(function(player, PlayerToBlind)
if not player:IsInGroup(7465879) then return false end;
script.BL:Clone().Parent = PlayerToBlind:WaitForChild("PlayerGui")
print("Done")
end)
Basically what I try to reach is if my Admin Panel Remoteevent is fired, and a Target has been chosen, the Targeted Player will become a Cloned GUI into their PlayerGui
Any fix on this error?
The error message itself is telling you that you are calling a function that doesn't exist. This issue is caused by an object not being the type you expect.
Unfortunately, the message is pointing at a line that has a few function calls, so it's difficult to say what is causing the exact error. Either script.BL isn't an object with a Clone() function, or PlayerToBlind isn't an object with a WaitForChild() function.
If you can break the operations into a few different lines and add safety checks along the way, you can guarantee that your code is safe and being called properly.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Blinder = ReplicatedStorage.RCD.Blind
local BL = script.BL
Blinder.OnServerEvent:Connect(function(player, PlayerToBlind)
-- check that the person calling Blinder:FireServer actually provided a PlayerToBlind
assert(type(PlayerToBind) == "string", "Blinder expects PlayerToBind to be the name of a player")
-- escape if the player is not in the appropriate group
if not player:IsInGroup(7465879) then
return
end
-- find the Player object based on the name provided from PlayerToBlind
local blindedPlayer = Players:FindFirstChild(PlayerToBlind)
if not blindedPlayer then
warn("Could not find player named : ", PlayerToBlind)
return
end
-- create a blinding UI on the other player
local targetGui = blindedPlayer:WaitForChild("PlayerGui")
local newBL = BL:Clone()
newBL.Parent = targetGui
print("Done")
end)
By adding asserts into your code, it won't stop your code from breaking, in fact it will force your code to break faster and in expected ways.
The issue has been fixed,
I have been setting up the LocalScript wrong.
(I grabbed the Text from a TextButton, Instead of the TextBox)

How do you create a light inside the player?

Im trying to create a light when the player spawns in but im running into multiple problems
I've tried many formats and functions always keep getting either
"Attempt to index nil value"
"[head(and other things)] is not a member of [humanoid(and other things)]"
game.Players.PlayerAdded:Connect(function(playersdude)
playersdude.CharacterAdded:Connect(function(char)
local humanoid = char:WaitForChild("Humanoid")
local light = Instance.new("PointLight")
light.Parent = game.Players.LocalPlayer.HumanoidRootPart
end)
end)
You're running into the same problem as this guy : (attempt to index field 'LocalPlayer' (a nil value))
I'm assuming that you've written this in a Script somewhere. LocalPlayercan only be accessed in a LocalScript. Trying to access it from a server Script will result in LocalPlayer being nil. Luckily, you don't need to use LocalPlayer at all!
You can use the char provided in the CharacterAdded connection to find the player's head.
game.Players.PlayerAdded:Connect(function(playersdude)
playersdude.CharacterAdded:Connect(function(char)
-- search through the character model to find the head
local head = char:FindFirstChild("Head", true)
-- add a light bright enough to make them glow like the mid-morning sun
local light = Instance.new("PointLight", head)
light.Brightness = 100
end)
end)